2011-01-13 254 views
1

我有以下2個文件,並在linux(debian)上執行它們。PHP執行命令作爲子命令

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 

回答

0

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"; 
-1

include('file2.php');

它應該立即執行命令

+1

那不是我要求所有..請再讀一遍! – Thomaschaaf 2011-01-13 15:44:51