Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Need help about Linux daemon and system call (system or popen)
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



Report this thread to moderator Post Follow-up to this message
Old Post
Guillaume Métayer
09-28-04 09:12 PM


Re: Need help about Linux daemon and system call (system or popen)
"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 t
he
> 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 b
e
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Jens.Toerring@physik.fu-berlin.de
09-28-04 09:12 PM


Re: Need help about Linux daemon and system call (system or popen)
* "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'

Report this thread to moderator Post Follow-up to this message
Old Post
Ralf Fassel
09-28-04 09:12 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:29 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.