【发布时间】:2013-04-05 05:47:51
【问题描述】:
从我的 apache 服务器的 PHP 页面,我使用如下行运行一些命令:
exec("{$command} >> /tmp/test.log 2>&1 & echo -n \$!");
你可以看到参数here的解释。
但我不明白一些事情:如果我重新启动或停止我的 apache 服务器,我的命令也会死掉。
root@web2:/sx/temp# ps ax | grep 0ff | grep -v grep
15957 ? S 0:38 /usr/bin/php /sx/site_web_php/fr_FR/app/console task:exec /sx/temp/task_inventaire/ 0ff79bf690dcfdf788fff26c259882e2d07426df 10800
root@web2:/sx/temp# /etc/init.d/apache2 restart
Restarting web server: apache2 ... waiting ..
root@web2:/sx/temp# ps ax | grep 0ff | grep -v grep
root@web2:/sx/temp#
经过一些研究,我阅读了一些关于父 pid 的内容,但是在我的命令行中使用 &,我认为我真的将我的子进程与他的父进程分离了。
我将 apache2 与 libapache2-mod-php5 和 apache2-mpm-prefork 一起使用。
我怎样才能真正将我的子程序与 apache 分离?
编辑
您可以通过这种方式在 Linux/Mac 上重现它:
a) 创建一个 execute_script.php 文件,其中包含:
<?php
sleep(10);
b) 创建一个 execute_from_http.php 文件,其中包含:
<?php
exec("php executed_script.php > /tmp/test.log 2>&1 & echo -n \$!");
c) 运行http://localhost/path/execute_from_http.php
d) 在终端上,运行命令:
ps axjf | grep execute | grep -v grep ; sudo /etc/init.d/apache2 restart ; ps axjf | grep execute | grep -v grep
如果您在 execute_from_http.php 脚本的 10 秒内运行该命令,您将获得输出:
php@beast:/var/www/xxx/$ ps axjf | grep execute | grep -v grep ; sudo /etc/init.d/apache2 restart ; ps axjf | grep execute | grep -v grep
1 5257 5245 5245 ? -1 S 33 0:00 php executed_script.php
* Restarting web server apache2
... waiting ...done.
php@beast:/var/www/xxx/$
如您所见,ps 命令只输出一次,这告诉您执行的脚本在 apache 重新启动时死亡。
【问题讨论】:
标签: php process exec parent-child daemon