Page 1 of 1

controlling a php-deamon, sort of

Posted: Wed Jan 08, 2003 5:01 pm
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

Posted: Wed Jan 08, 2003 7:10 pm
by volka
you can check the output of

Code: Select all

exec('ps -ax | grep "whatYoureLookingFor"', $output);

ok..?

Posted: Thu Jan 09, 2003 6:14 am
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.

Posted: Thu Jan 09, 2003 7:55 am
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:

ok..?

Posted: Thu Jan 09, 2003 9:10 am
by piuga
Ok, so "WhatYourLookingFor" should be replaced with the name of my script?

/t

Posted: Thu Jan 09, 2003 10:37 am
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...

Posted: Thu Jan 09, 2003 11:09 am
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];
   ...

Thanks alot!

Posted: Thu Jan 09, 2003 11:28 am
by piuga
Thanks, now I get it!