PHP 将命令作为子命令执行

发布于 2024-10-11 13:08:40 字数 917 浏览 2 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2024-10-18 13:08:40

我没有过多研究它,但我的第一个猜测是从 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.

耶耶耶 2024-10-18 13:08:40

popen 和 proc_open 似乎可以做到这一点:)

file1.php

<?php
$descs = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "/tmp/error-output.txt", "a")
);
$process = proc_open("php -f file2.php", $descs, $pipess);
sleep(100);

file2.php

<?php
$desc = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "/tmp/error-output.txt", "a")
);
echo "asd";
$process[] = proc_open("sleep 12", $desc, $pipes);
echo "sleep!";
$process[] = proc_open("sleep 10", $desc, $pipes);
echo "asd";

popen and proc_open seem to do it :)

file1.php

<?php
$descs = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "/tmp/error-output.txt", "a")
);
$process = proc_open("php -f file2.php", $descs, $pipess);
sleep(100);

file2.php

<?php
$desc = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "/tmp/error-output.txt", "a")
);
echo "asd";
$process[] = proc_open("sleep 12", $desc, $pipes);
echo "sleep!";
$process[] = proc_open("sleep 10", $desc, $pipes);
echo "asd";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文