PHP执行命令和日志输出,无需等待
我使用exec()执行Linux或Windows命令.
I use exec() to execute command, either linux or windows.
如何执行命令,Linux和Windows,并在不等待的情况下记录输出?
How do you execute a command, linux and windows, and log the output without waiting?
对于Linux,我知道不要等待输出:command* > /dev/null 2>/dev/null &
I know for linux, to not wait for the output: command* > /dev/null 2>/dev/null &
并记录Linux的输出:command* > /path/to/log.txt 2>/path/to/error.txt
And to log output for linux: command* > /path/to/log.txt 2>/path/to/error.txt
如何使用一个命令记录日志并将其设置为后台?窗户看起来如何?
How would you go about logging and setting it to background in one command? How would windows look like too?
在Linux上,您可以执行以下操作:
On Linux you can do:
exec('command* > /dev/null 2>/dev/null &');
在Windows上,您可以执行以下操作:
On Windows you can do:
pclose(popen('start /B cmd /C "command* >NUL 2>NUL"', 'r'));
这两个示例均禁用输出和错误,这些错误将输出到/dev/null
(linux)或NUL
(windows),这意味着它们无处存储".
Both examples disable output and errors, those go to /dev/null
(linux) or NUL
(windows) which means they are stored "nowhere".
您可以将它们替换为系统上的有效路径.
You can replace these with valid paths on your system.
在Linux上,最后的&
将其置于后台.在Windows上,这更加复杂,需要 start
来调用该过程,并需要
On Linux, a &
at the end places it into background. On windows this is more complicated and needs start
to invoke the process and cmd
to allow redirection of the streams.