PHP 将命令作为子命令执行
我有以下 2 个文件,并在 linux (debian) 上执行它们。
File1.php
<?php
exec("php -f file2.php > /dev/null 2>&1 &");
sleep(100);
File2.php
<?php
exec("sleep 30 > /dev/null 2>&1 &");
exec("sleep 30 > /dev/null 2>&1 &");
sleep(100);
目前的方式是首先启动 file1.php 并启动 file2.php 并立即开始睡眠命令。它不会等待第一个睡眠完成即可继续。
问题是 file2.php 和 sleep 命令不是 file1.php 的子命令,我不能简单地杀死它来杀死所有子命令。 我的 htop 看起来像这样: http://dl.getdropbox.com /u/5910/Jing/2011-01-13_1611.png
我正在寻找一种方法,让这些命令成为 file1.php 的子命令,这样我就可以轻松地将它们全部杀死:)
我所拥有的:
'- php -f file2.php
'-sleep 30
'-sleep 30
'- php -f file1.php
基本上我想要这个:
'- php -f file1.php
'- php -f file2.php
'-sleep 30
'-sleep 30
I have the following 2 files and am executing them on linux (debian).
File1.php
<?php
exec("php -f file2.php > /dev/null 2>&1 &");
sleep(100);
File2.php
<?php
exec("sleep 30 > /dev/null 2>&1 &");
exec("sleep 30 > /dev/null 2>&1 &");
sleep(100);
The way it currently is it first starts file1.php and fires up file2.php and immediatly begins the sleep commands. It does not wait for the first sleep to finish to continue.
The problem is that the file2.php and sleep commands are not subcommands of file1.php and I can't simply kill it to kill all subcommands.
My htop looks like this: http://dl.getdropbox.com/u/5910/Jing/2011-01-13_1611.png
I am searching for a way to have the commands be subcommands of file1.php so that I can easily kill them all :)
what I have:
'- php -f file2.php
'-sleep 30
'-sleep 30
'- php -f file1.php
basically I want this:
'- php -f file1.php
'- php -f file2.php
'-sleep 30
'-sleep 30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有过多研究它,但我的第一个猜测是从 file1 中分叉它。
http://php.net/manual/en/function.pcntl-fork.php
我稍后可以更好地查看,但是您应该能够使用 pcntl_waitpid($pid) 来完成 fork。
I haven't looked into it too much, but my first guess would be to fork it from file1.
http://php.net/manual/en/function.pcntl-fork.php
I can take a better look later, but you should be able to use pcntl_waitpid($pid) for the fork to complete.
popen 和 proc_open 似乎可以做到这一点:)
file1.php
file2.php
popen and proc_open seem to do it :)
file1.php
file2.php