controlling a php-deamon, sort of

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
piuga
Forum Newbie
Posts: 9
Joined: Fri May 10, 2002 11:24 am
Location: Relative to what?

controlling a php-deamon, sort of

Post by piuga »

Ok, here it is:

I wonder if there is any way to get the process-id of a backgroundprocess thrown in a php-script using exec.

So that I, on every 'some'-request check if a backgroundscript is running and if not make sure it does start running.


Background:
I'm building an alpha of a webgame which will be built in the following way:
-A deamon (written i php, since my host wont allow C-executables) that does things and updates a Db.
-A server that handles the user requests
-A Db to store the current status of a lot of things, and also stores tasks for the deamon to do,
-A web-app (also written in php) that queries the Db for data and also writes tasks for the deamon in the Db.

/Tomas
Last edited by piuga on Thu Jan 09, 2003 6:12 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you can check the output of

Code: Select all

exec('ps -ax | grep "whatYoureLookingFor"', $output);
User avatar
piuga
Forum Newbie
Posts: 9
Joined: Fri May 10, 2002 11:24 am
Location: Relative to what?

ok..?

Post by piuga »

Ok, and what does it tell me?

Is it possible to get the process-id when I've started the process, save it in a Db and then check for it?

Sorry if this doesn't make sense, but I'm a total linux/unix newbie.
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

Well, in the example given above the first thing on the list is the proccess ID so just grab it and leave the rest alone...

As for what your avatar looks like...Hmmm...heh...looks like a big purple fish to me! :twisted:
User avatar
piuga
Forum Newbie
Posts: 9
Joined: Fri May 10, 2002 11:24 am
Location: Relative to what?

ok..?

Post by piuga »

Ok, so "WhatYourLookingFor" should be replaced with the name of my script?

/t
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

It would be the name of the process you are trying to find, example:

Code: Select all

If you are trying to find the MySQL Daemon you would put in:

ps ax mysqld
that's all there is too it...Enjoy...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if you have invoked another php script in background settting "WhatYourLookingFor" to the script's name should return something like 12678 /usr/bin/php -f /path/scriptname if the script is still running. If not $output should be empty.
If you still need to extract the pid you might do so with

Code: Select all

exec('ps -ax | grep "whatYoureLookingFor"', $output);
if (count($output) > 0)
{
   $pid = explode(' ', $output[0]);
   $pid = $pid[0];
   ...
User avatar
piuga
Forum Newbie
Posts: 9
Joined: Fri May 10, 2002 11:24 am
Location: Relative to what?

Thanks alot!

Post by piuga »

Thanks, now I get it!
Post Reply