Launching a PHP daemon from an LSB init script w/ start-stop-daemon

Posted by EvanK on Server Fault See other posts from Server Fault or by EvanK
Published on 2011-02-01T17:51:30Z Indexed on 2012/12/19 17:04 UTC
Read the original article Hit count: 426

Filed under:
|
|
|
|

I'm writing an lsb init script (admittedly something I've never done from scratch) that launches a php script that daemonizes itself. The php script starts off like so:

#!/usr/bin/env php
<?php
/* do some stuff */

It's then started like so in the init script:

# first line is args to start-stop-daemon, second line is args to php-script
start-stop-daemon --start --exec /path/to/executable/php-script.php \
  -- --daemon --pid-file=$PIDFILE --other-php-script-args

The --daemon flag causes the php script to detach & run as a daemon itself, rather than relying on start-stop-daemon to detach it.

This is how it's (trying to) stop it in the init script:

start-stop-daemon --stop --oknodo --exec /path/to/executable/php-script.php \
  --pidfile $PIDFILE

The problem is, when I try to stop via the init script, it gives me this:

$ sudo /etc/init.d/my-lsb-init-script stop
 * Stopping My Project
No /path/to/executable/php-script.php found running; none killed.
   ...done.

A quick peek at ps tells me that, even though the php script itself is executable, its running as php <script> rather than the script name itself, which is keeping start-stop-daemon from seeing it. The PID file is even being generated, but it seems to ignore it and try to find+kill by process name instead.

$ ps ax | grep '/path/to/executable/php-script.php'
 2505 pts/1    S      0:01 php /path/to/executable/php-script.php --daemon --pid-file /var/run/blah/blah.pid --other-php-script-args
 2507 pts/1    S      0:00 php /path/to/executable/php-script.php --daemon --pid-file /var/run/blah/blah.pid --other-php-script-args
 2508 pts/1    S      0:00 php /path/to/executable/php-script.php --daemon --pid-file /var/run/blah/blah.pid --other-php-script-args
 2509 pts/1    S      0:00 php /path/to/executable/php-script.php --daemon --pid-file /var/run/blah/blah.pid --other-php-script-args
 2518 pts/1    S      0:01 php /path/to/executable/php-script.php --daemon --pid-file /var/run/blah/blah.pid --other-php-script-args
$ cat /var/run/blah/blah.pid
2518

Am I completely misunderstanding something here? Or is there an easy way to work around this?

© Server Fault or respective owner

Related posts about php

Related posts about bash