Shell_exec php与nohup

时间:2011-02-08 05:14:34

标签: php exec shellexecute shell-exec nohup

我认为有很多类似的帖子,但我在搜索后还没有找到解决方案。

基本上,我正在尝试在后台运行两个脚本。当我在命令行中运行它们时,我会在调用我的第一个脚本后看到:

/usr/bin/nohup php script.php > nohupoutput.log & echo $!

我尝试了...script.php > /dev/null &,结果相同。我明白了:

/usr/bin/nohup: ignoring input and redirecting stderr to stdout

我忽略并运行第二个。我注意到它似乎挂在那里,按 Enter 让我回到machine:~folder>

/usr/bin/nohup php script2.php > nohupoutput.log & echo $!

两个脚本都有效。我试图将其转换为shell_exec命令,似乎没有任何工作。我怀疑ignoring input位造成了困难,但我不确定。无论如何,以下不起作用。它只是挂在浏览器中:

$output = shell_exec('/usr/bin/nohup php script.php > /dev/null &');
$output = shell_exec('/usr/bin/nohup php script2.php > /dev/null &');

3 个答案:

答案 0 :(得分:9)

尝试:

$output = shell_exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');

或者:

exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');

答案 1 :(得分:2)

这应该有效:

shell_exec('nohup /usr/bin/php path/to/script.php > output.txt &');

答案 2 :(得分:2)

this would help you dude..


<?php

function execInBackground($cmd) {
    if (substr(php_uname(), 0, 7) == "Windows"){
        pclose(popen("start /B ". $cmd, "r")); 
    }
    else {
        exec($cmd . " > /dev/null &");  
    }
}
//take note: to get your PHP_PATH, try looking at your phpinfo :)
echo execInBackground("/usr/local/php53/bin/php 'example2.php'");

?>

这是我的工作样本页面: () **** http://affiliateproductpromotions.net/pop.php ***** ()