从 php 启动独立进程(通过 httpd)
我试图在unix环境下从php生成一个类似守护进程的进程,保留pid以供参考,并使其完全独立于httpd进程。这对于从管理后台控制中间件应用程序非常有用。我想保持它的通用性以重用任何命令,我不想为每个命令编写 .sh 。
使用 php 的 exec() 我运行这个命令:
$command = "java /myApp/TestInfiniteLoop";
$log = "/myApp/myLog.txt";
$echos = null;
$fullCommand = "bash -c 'nohup $command < /dev/null > $log 2>&1 & echo $! & exit' & echo $!";
exec($fullCommand, $echos);
$pid = (int)$echos[0] + 2;
它返回 $pid 但不会生成进程(或立即杀死它......)。
如果我在 bash shell 上运行 $fullCommand ,它会完美运行。我可以关闭 bash,一切都会按预期运行。
如果我取出“nohup”它就可以工作(但是pid是+1而不是+2),但是一旦你停止httpd,进程也会被杀死...
使它工作的正确方法是什么?
如果可能的话,有人可以更正它以直接获取pid吗?我假设这是 bash (+2) 调用的第二个进程,但有可能(虽然不太可能,我知道......)它不会相同。
PS:我已经在Mac和Linux环境上对其进行了测试。
i'm trying to spawn a daemon-like process from php on a unix environment, keep the pid for reference and keep it completely independent from httpd processes. This is useful for controlling middleware applications from administrative backoffices. I want to keep it generalized to reuse for any command, i don't want to write a .sh for every command.
using php's exec() i'm running this command:
$command = "java /myApp/TestInfiniteLoop";
$log = "/myApp/myLog.txt";
$echos = null;
$fullCommand = "bash -c 'nohup $command < /dev/null > $log 2>&1 & echo $! & exit' & echo $!";
exec($fullCommand, $echos);
$pid = (int)$echos[0] + 2;
It returns $pid but does not spawn process (or kills it immediately...).
If i run the $fullCommand on a bash shell it works perfectly. I can close the bash and everything keeps running as expected.
If i take out the "nohup " it works (but pid is +1 and not +2), but as soon as you stop httpd the process get's killed too...
What is the correct way to make it work?
If possible can anyone correct it to get the pid directly? i'm assuming it is the second process from the bash (+2) call, but it is possible (though not probable, i know...) that it wouldn't be the same.
PS: i've been testing it on Mac and Linux environments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你几乎已经拥有了。我认为您可能有太多管道正在进行,并且 http 进程没有断开连接,因为它正在等待输出。这会起作用:
我一直使用 PHP 脚本执行此操作,您不必编写 shell 脚本。
You almost have it. I think you may have too many pipes going on and the http process isn't disconnecting because it's waiting for output. This will work:
I do this with PHP scripts all the time, you don't have to write a shell script.
我过去做过的一件事是已经运行一个单独的进程(如 python 或 c 应用程序)来监视文本文件或数据库。 php 进程写入文件或数据库,而作为具有单独权限的单独用户运行的另一个进程正在轮询该文件或数据库。当它看到要查找的内容时,它会执行所需的操作,将进度报告回文件或数据库。这样,您在应用程序的操作上就有了更多的自由度和灵活性(例如,它可以将其 pid 写入文件,以便 php 知道它是什么)。
One thing I've done in the past is have a separate process (like a python or c application) already running that watches either a text file or a database. The php process either writes to the file or database, and the other process, which is running as a separate user with separate permissions is polling that file or database. When it sees what it's looking for, it does whatever it needs to, reporting progress back to the file or database. That way you have a lot more freedom and flexibility in what the app does (it could write its pid to the file, for example, so that php knows what it is).
我会研究类似 gearman api,旨在创建长时间运行的进程(然后从中生成操作),而不是尝试自己生成应用程序并尝试找出 pid (它还有一个额外的好处,那就是更加跨平台)。 +2 pid肯定会出现竞争条件问题。
I would look into something like gearman api, which is designed to create long running processes (and then spawn actions from that) rather than try to spawn applications yourself and try to figure out the pid (it will have the side benifit of being a lot more cross platform as well). The +2 pid will definitely have race condition problems.