简体   繁体   中英

PHP execute command and log output without waiting

I use exec() to execute command, either linux or windows.

How do you execute a command, linux and windows, and log the output without waiting?

I know for linux, to not wait for the output: command* > /dev/null 2>/dev/null &

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?

On Linux you can do:

exec('command* > /dev/null 2>/dev/null &');

On Windows you can do:

pclose(popen('start /B cmd /C "command* >NUL 2>NUL"', 'r'));

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM