For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2004 > Need help about Linux daemon and system call (system or popen)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Need help about Linux daemon and system call (system or popen)
Guillaume Métayer

2004-09-28, 4:12 pm

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


Jens.Toerring@physik.fu-berlin.de

2004-09-28, 4:12 pm

"Guillaume Métayer" <guillaumeNOmetayerSPAM@hotmail.com> wrote:
> 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 ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Ralf Fassel

2004-09-28, 4:12 pm

* "Guillaume Métayer" <guillaumeNOmetayerSPAM@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'
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com