简体   繁体   English

输出重定向后无法杀死子进程

[英]Cannot kill children processes when the output redirected

I have a PHP script to run a command with execute time limit and cpu time limit. 我有一个PHP脚本来运行具有执行时间限制和cpu时间限制的命令。

limit.php : limit.php

<?php
declare(ticks = 1);
ini_set('display_errors','on');
error_reporting(E_ALL);

if ($argc<2) die("Usage: php ".__FILE__." \"your command here\" [execute_time_limit = 600] [cpu_time_limit = 120]\n");
$cmd = $argv[1];
$tl = isset($argv[2]) ? intval($argv[2]) : 0;
$cl = isset($argv[3]) ? intval($argv[3]) : 0;
if (!$cmd) die("INCORRECT_CMD");
if ($tl<=0) $tl = 600;
if ($cl<=0) $cl = 120;

function sigalrm_handler($signo) {die('EXECUTE_ENDED');}//normal ended or killed by ulimit

$pid = pcntl_fork();
if (-1 == $pid) {
    die('FORK_FAILED');
} elseif ($pid == 0) {
    exec("ulimit -t $cl && $cmd");
    posix_kill(posix_getppid(), SIGALRM);
} else {
    pcntl_signal(SIGALRM, 'sigalrm_handler');
    sleep($tl);
    posix_kill($pid, SIGKILL);
    die("TIMEOUT_KILLED : $pid");
}

Another PHP script for test. 另一个用于测试的PHP脚本。

test.php test.php

<?php
$i=0;
echo getmypid();
while(1) {
    $i++;
    file_put_contents("/tmp/x.log",$i."\n",FILE_APPEND);
    echo "*";
    sleep(1);
}

Run this command: 运行以下命令:

php limit.php "php test.php" 10 php limit.php“ php test.php” 10

Use "ps -ef|grep ph[p]", you can find processes forked and killed as we expected. 使用“ ps -ef | grep ph [p]”,您可以按我们的预期找到分叉和终止的进程。

But when I try this command, 但是当我尝试这个命令时

php limit.php "php test.php > /tmp/y.log" 10 php limit.php“ php test.php> /tmp/y.log” 10

the children processes cannot be killed as we expected. 子进程不能像我们预期的那样被杀死。 See: 看到:

Begining: 开始:

=============== ===============

alix     28979 23528  0 17:32 pts/0    00:00:00 php limit.php php test.php > /tmp/y.log 10
alix     28980 28979  0 17:32 pts/0    00:00:00 php limit.php php test.php > /tmp/y.log 10
alix     28981 28980  0 17:32 pts/0    00:00:00 sh -c ulimit -t 120 && php test.php > /tmp/y.log
alix     28982 28981  0 17:32 pts/0    00:00:00 php test.php

after 10s: 10秒后:

================= =================

alix     28981     1  0 17:32 pts/0    00:00:00 sh -c ulimit -t 120 && php test.php > /tmp/y.log
alix     28982 28981  0 17:32 pts/0    00:00:00 php test.php

PID 28979 forked 28980, then function exec created 2 children process with PID 28981 and 28982. 10s later, 28979 send SIGKILL to 28980, but 28981 and 28982 were not impacted (they will be killed if there is not output redirect like "> /tmp/y.log"). PID 28979分叉了28980,然后函数exec用PID 28981和28982创建了2个子进程。10s之后,28979将SIGKILL发送到28980,但是28981和28982没有受到影响(如果没有输出重定向,例如“> / tmp,它们将被杀死。 /y.log”)。

My server is : Linux s4 2.6.18-308.1.1.el5 #1 SMP Wed Mar 7 04:16:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux With PHP 5.3.9 (cli) (built: Feb 15 2012 11:54:46) 我的服务器是:Linux s4 2.6.18-308.1.1.el5#1 SMP Wed Mar 7 03 7 04:16:51 EST 2012 x86_64 x86_64 x86_64 GNU / Linux With PHP 5.3.9(cli)(build:Feb 15 2012 11: 54:46)

Any suggestion? 有什么建议吗?

The reason is PIPE . 原因是PIPE Check the answer in another question of mine: 在另一个问题中检查答案:

standard output impact SIGKILL? 标准输出影响SIGKILL?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM