从PHP启动守护程序

从PHP启动守护程序

本文介绍了从PHP启动守护程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于网站,我需要能够启动和停止守护进程.我目前正在做的是

For a website, I need to be able to start and stop a daemon process. What I am currently doing is

exec("sudo /etc/init.d/daemonToStart start");

守护进程已启动,但Apache/PHP挂起.进行ps aux显示sudo本身变成了僵尸进程,有效地杀死了所有进一步的进程.尝试从PHP启动daeomon时,这是正常现象吗?

The daemon process is started, but Apache/PHP hangs. Doing a ps aux revealed that sudo itself changed into a zombie process, effectively killing all further progress. Is this normal behavior when trying to start a daeomon from PHP?

是的,Apache有权执行/etc/init.d/daemonToStart命令.我更改了/etc/sudoers文件以允许它这样做.不,我不允许Apache执行任何类型的命令,只有少数几个可以使网站正常工作.

And yes, Apache has the right to execute the /etc/init.d/daemonToStart command. I altered the /etc/sudoers file to allow it to do so. No, I have not allowed Apache to be able to execute any kind of command, just a limited few to allow the website to work.

无论如何,回到我的问题,有没有一种方法允许PHP以不创建任何僵尸进程的方式启动守护进程?我之所以这样问,是因为当我做相反的事情时,停止一个已经启动的守护进程就可以了.

Anyway, going back to my question, is there a way to allow PHP to start daemons in a way that no zombie process is created? I ask this because when I do the reverse, stopping an already started daemon, works just fine.

推荐答案

尝试将> /dev/null 2>&1 &附加到命令中.

所以这个:

exec("sudo /etc/init.d/daemonToStart > /dev/null 2>&1 &");

以防万一,您想知道它的作用/原因:

Just in case you want to know what it does/why:

  • > /dev/null-将STDOUT重定向到/dev/null(换句话说,它是黑洞的)
  • 2>&1-将STDERR重定向到STDOUT(也将其黑洞)
  • &分离过程并在后台运行
  • > /dev/null - redirect STDOUT to /dev/null (blackhole it, in other words)
  • 2>&1 - redirect STDERR to STDOUT (blackhole it as well)
  • & detach process and run in the background

这篇关于从PHP启动守护程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:53