我正在写一个LSB初始化脚本(无可否认,我从来没有从头开始)启动一个PHP脚本,守护自己。 PHP脚本开始如下所示:
#!/usr/bin/env php <?php /* do some stuff */
然后在init脚本中这样启动:
# 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
--daemon标志会导致php脚本作为一个守护进程自行分离和运行,而不是依靠start-stop-daemon来分离它。
这是如何(试图)停止在init脚本中:
start-stop-daemon --stop --oknodo --exec /path/to/executable/php-script.php \ --pidfile $PIDFILE
问题是,当我试图停止通过初始化脚本,它给了我这个:
$ sudo /etc/init.d/my-lsb-init-script stop * Stopping My Project No /path/to/executable/php-script.php found running; none killed. ...done.
在ps快速浏览一下,即使php脚本本身是可执行文件,但它的运行方式是php <script>而不是脚本名称本身,这使得start-stop-daemon不能看到它。 PID文件甚至正在生成,但它似乎忽略它,并尝试find+杀进程名称。
$ 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
我完全误解了这里的东西吗? 还是有一个简单的方法来解决这个问题?
正确停止:
start-stop-daemon --stop --oknodo --pidfile $PIDFILE