php exec 命令(或类似命令)不等待结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3819398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php exec command (or similar) to not wait for result
提问by greg
I have a command I want to run, but I do not want PHP to sit and wait for the result.
我有一个要运行的命令,但我不想让 PHP 坐等结果。
<?php
echo "Starting Script";
exec('run_baby_run');
echo "Thanks, Script is running in background";
?>
Is it possible to have PHP not wait for the result.. i.e. just kick it off and move along to the next command.
是否有可能让 PHP 不等待结果.. 即只是踢它并继续下一个命令。
I cant find anything, and not sure its even possible. The best I could find was someone making a CRON job to start in a minute.
我找不到任何东西,甚至不确定它是否可能。我能找到的最好的是有人在一分钟内开始做 CRON 工作。
回答by Cristian
From the documentation:
从文档:
In order to execute a command and have it not hang your PHP script while
it runs, the program you run must not output back to PHP. To do this,
redirect both stdout and stderr to /dev/null, then background it.
> /dev/null 2>&1 &
In order to execute a command and have
it spawned off as another process that
is not dependent on the Apache thread
to keep running (will not die if
somebody cancels the page) run this:
exec('bash -c "exec nohup setsid your_command > /dev/null 2>&1 &"');
为了执行命令并让它在
运行时不挂起您的 PHP 脚本,您运行的程序不能输出回 PHP。为此,
请将 stdout 和 stderr 都重定向到 /dev/null,然后将其设为后台。
> /dev/null 2>&1 &
为了执行命令并将
它作为另一个
不依赖于 Apache 线程
继续运行的进程(如果
有人取消页面不会死)产生,请运行以下命令:
exec('bash -c "exec nohup setsid your_command > /dev/null 2>&1 &"');
回答by codaddict
You can run the command in the background by adding a &
at the end of it as:
您可以通过&
在其末尾添加 a 来在后台运行该命令:
exec('run_baby_run &');
But doing this alone will hang your script because:
但是单独执行此操作会挂起您的脚本,因为:
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
如果程序是用 exec 函数启动的,为了让它在后台继续运行,程序的输出必须重定向到一个文件或另一个输出流。如果不这样做,将导致 PHP 挂起,直到程序执行结束。
So you can redirect the stdout of the command to a file, if you want to see it later or to /dev/null
if you want to discard it as:
因此,您可以将命令的标准输出重定向到一个文件,如果您想稍后查看它或者/dev/null
您想将其丢弃为:
exec('run_baby_run > /dev/null &');
回答by Paul T
This uses wget to notify a URL of something without waiting.
这使用 wget 无需等待即可通知某个 URL。
$command = 'wget -qO- http://test.com/data=data';
exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid);
This uses ls to update a log without waiting.
这使用 ls 无需等待即可更新日志。
$command = 'ls -la > content.log';
exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid);
回答by Anil
I know this question has been answered but the answers i found here didn't work for my scenario ( or for Windows ).
我知道这个问题已经得到回答,但我在这里找到的答案不适用于我的场景(或 Windows )。
I am using windows 10 laptop with PHP 7.2 in Xampp v3.2.4.
我在 Xampp v3.2.4 中使用带有 PHP 7.2 的 Windows 10 笔记本电脑。
$command = 'php Cron.php send_email "'. $id .'"';
if ( substr(php_uname(), 0, 7) == "Windows" )
{
//windows
pclose(popen("start /B " . $command . " 1> temp/update_log 2>&1 &", "r"));
}
else
{
//linux
shell_exec( $command . " > /dev/null 2>&1 &" );
}
This worked perfectly for me.
这对我来说非常有效。
I hope it will help someone with windows. Cheers.
我希望它能帮助有窗户的人。干杯。
回答by Aleksandr Ryabov
There are two possible ways to implement it. The easiest way is direct result to dev/null
有两种可能的方法来实现它。最简单的方法是直接结果为 dev/null
exec("run_baby_run > /dev/null 2>&1 &");
But in case you have any other operations to be performed you may consider ignore_user_abortIn this case the script will be running even after you close connection.
但如果您有任何其他操作要执行,您可以考虑ignore_user_abort在这种情况下,即使关闭连接,脚本也会运行。
回答by jobeard
"exec nohupsetsid your_command"
“exec nohupsetsid your_command”
the nohupallows your_command to continue even though the process that launched may terminate first. If it does, the the SIGNUP signal will be sent to your_command causing it to terminate (unless it catches that signal and ignores it).
在nohup的允许your_command继续即使是推出可先终止进程。如果是, SIGNUP 信号将被发送到 your_command 导致它终止(除非它捕获该信号并忽略它)。