10

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?

3
  • 1
    You can exec() including redirecting output to a file, and then read the file via PHP later on. Commented Jun 28, 2012 at 18:32
  • 1
    The & at the end put things in background. But that does not work in windows, that's linux. Commented Jun 28, 2012 at 18:34
  • In windows, I believe you just use start Commented Jun 28, 2012 at 18:35

1 Answer 1

23

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.

Sign up to request clarification or add additional context in comments.

6 Comments

You could direct output to a file, and then fopen/!feof the log file
For linux, how would you go about logging and setting it to background in one command?
@user1105430: That has been answered in the answer. See the first line.
@hakre so I can use "command* > /path/to/log.txt 2>/path/to/error.txt &"?
It isn't mentioned, but you've to replace the entire command* (including the *) with your command. If you'll keep the *, it won't work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.