Shell_exec php with nohup

别说谁变了你拦得住时间么 提交于 2019-12-09 02:59:27

问题


I think there are tons of similar posts but I haven't yet found a solution after searching around.

Basically, I'm trying to run two scripts in the background. When I run them in the commandline, I see after calling my first script:

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

I've tried ...script.php > /dev/null & with the same result. I get:

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

which I ignore and run the second one. I noticed that it seemed to be hanging there, and pressing Enter brought me back to machine:~folder>

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

Both scripts work. I tried to then convert this to a shell_exec command and nothing seems to work. I suspect that the ignoring input bit is causing difficulties, but I'm not sure. Regardless, the following does not work. It just hangs in the browser:

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

回答1:


Try:

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

Or:

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



回答2:


This shoul work:

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



回答3:


<?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'");
?>


来源:https://stackoverflow.com/questions/4929827/shell-exec-php-with-nohup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!