PHP Exec: Without Waiting, Without Discarding the Output, Without nohup

浪子不回头ぞ 提交于 2020-02-07 05:49:27

问题


I need to run a command in PHP like this:

exec('dosomething > saveit.txt');

Except I don't want PHP to wait for it to be complete. I also don't want to throw away the output, and I don't want to use nohup because I'm using that for something else in the same directory.

I also tried pclose(popen('dosomething > saveit.txt','r')); and that didn't work, it still waited.


回答1:


Add an ampersand to the end of the command, so:

exec('dosomething > saveit.txt &');



回答2:


in the documentation of exec() there is an interesting comment that says:

Took quite some time to figure out the line I am going to post next. If you want to execute a command in the background without having the script waiting for the result, you can do the following:

 <?php
  passthru("/usr/bin/php /path/to/script.php ".$argv_parameter." >> /path/to/log_file.log 2>&1  &");
 ?>


来源:https://stackoverflow.com/questions/8062839/php-exec-without-waiting-without-discarding-the-output-without-nohup

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