Re: Start/stop daemon using php
| From: | @4u | Date: | Wed, 27 Feb 2008 14:08:55 +0000 |
| Subject: | Re: Start/stop daemon using php | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-270284@lists.php.net to get a copy of this message | ||
Hi,
You might consider D-BUS for your application and the D-BUS PHP binding
which is available since some days too. This would allow you to start /
stop your C application in a far more secure way than the suggested one.
Please have a look at my original release annoucement at the D-BUS
mailing list:
http://lists.freedesktop.org/archives/dbus/2008-February/009363.html
as well as the download URL:
https://sourceforge.net/project/showfiles.php?group_id=17176&package_id=68954
Even if the application runs on Windows you might be able to use D-BUS
for communication.
Daniel Brown schrieb:
> On Wed, Feb 27, 2008 at 7:51 AM, David Sveningsson <ext@sidvind.com> wrote:
>> Hi, I've written an application in c which I would like to start/stop as
>> a daemon in gnu/linux.
>>
>> The application has the argument "--daemon" which forks the process and
>> exits the parent. Then it setups a SIGQUIT signal handler to properly
>> cleanup and terminate. It also maintains a lockfile (with the pid) so
>> only one instance is allowed.
>>
>> So, to start this application I created a php site that calls
>> exec("/path/to/binary --daemon > /dev/null 2> /dev/null").
>
> You can (and should) write this out like so:
> <?
> exec("/path/to/binary --daemon >> /dev/nul 2>&1",$ret,$err);
> ?>
>
> The >> means to append to the end of a file. You can use > on
> /dev/null, since it's not a file, but just a black hole, but you may
> want to get into the habit of redirecting and appending. The 2>&1
> redirects channel 2 (STDERR) to channel 1 (STDOUT) so that all output
> in this case is sent to /dev/null. For any output that would
> otherwise be generated, $ret will hold STDOUT data, and $err will hold
> the STDERR code.
>
>> Everything is working so far, but I cannot get the application to
>> receive the SIGQUIT when I start using php and exec. Not even manually
>> using kill in the shell. It works correctly if I start manually thought.
>>
>> So, is this possible to do? Doesn't exec allow applications with signal
>> handlers? Is there some other way to terminate the application?
>
> You may want to launch it from a BASh script. I had written out
> an example for someone on this list at the beginning of the month.
> Feel free to check it out and use it, or use any part of it:
>
> [Download] http://pilotpig.net/code-library/daemonize.sh
> [View Source]
> http://pilotpig.net/code-library/source.php?f=daemonize.sh
>