Discussion:
Need help about Linux daemon and system call (system or popen)
(too old to reply)
Guillaume Métayer
2004-09-28 15:55:22 UTC
Permalink
Hello,

That's a long story but there's a PHP script I need to call in the
background, and the PHP script *never returns*, meaning it's running all the
time (I stop it with Ctrl-C when running it from the console) so I decided
to make a small daemon in C (GCC) on Linux. The daemon simply calls
system("php myscript.php"). The problem is that I can't seem to find a way
to have the PHP process killed when I shutdown my daemon. I tried using
popen or different techniques but basically, when I kill my daemon process,
it doesnt kill the PHP process. My understanding of Unix processes might be
limited but I thought that anything created under a process would die
(unless created as daemon) when parent dies. I know that system uses sh so
sh is the parent, but wouldnt the deamon be sh's parent? Anyway, any help
would be appreciated, thanks,


------
Guillaume
J***@physik.fu-berlin.de
2004-09-28 18:01:25 UTC
Permalink
Post by Guillaume Métayer
That's a long story but there's a PHP script I need to call in the
background, and the PHP script *never returns*, meaning it's running all the
time (I stop it with Ctrl-C when running it from the console) so I decided
to make a small daemon in C (GCC) on Linux. The daemon simply calls
system("php myscript.php"). The problem is that I can't seem to find a way
to have the PHP process killed when I shutdown my daemon. I tried using
popen or different techniques but basically, when I kill my daemon process,
it doesnt kill the PHP process. My understanding of Unix processes might be
limited but I thought that anything created under a process would die
(unless created as daemon) when parent dies.
No that's not the way it happens. With system() you create a new
process that runs your PHP script with the program that called
system() just hanging around, waiting for the newly started
process to die. Killing it does not also kill its child process
(actually, killing a parent process does not kill its children
daemon or not). You should replace your program that now calls
system() by the new program to run, in your case php. That's done
via one of the functions from the exec() family. Just replace your
system() call by

#include <unistd.h>
char *args[ ] = { "php", "php", "myscript.php", NULL" };
execlp( args );

Instead of having the "daemon" still hanging around its code
will now get replaced by php, running your myscript.php script
and no new process is created, (Note that having the "php" string
twice in args is required.)
Regards, Jens
--
\ Jens Thoms Toerring ___ ***@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Ralf Fassel
2004-09-28 18:14:54 UTC
Permalink
* "Guillaume Métayer" <***@hotmail.com>
| system("php myscript.php"). The problem is that I can't seem to find
| a way to have the PHP process killed when I shutdown my daemon.

The usual way is to make the PHP script (or any program) exit when
stdin is closed.

| My understanding of Unix processes might be limited but I thought
| that anything created under a process would die (unless created as
| daemon) when parent dies.

Not unless you add code to achieve that. See your manual about
process groups for details.

R'

Loading...