views:

1905

answers:

4

Hello all,

In the php system command we use the following

system("ffmpeg -i test.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv output_file.flv 2>&1 &").

Please explain the usage of the above mentioned system command. What does '2>&1 &' stand for ? I want to dump the process details to a file how do I do that ?

Thank you very much.

+8  A: 

2>&1 redirects «stderr» to «stdout», & at the end makes the command run in background.

To make it complete it should be

«command» 2>&1 > /tmp/somefile.log &

Without redirecting «stdout» to a file (or to /dev/null) running in background from system() doesn't make much sense, as your command would get killed as soon as PHP terminates (e.g. reaching time limit).

From system() manual:

Note: If a program is started with this 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.

vartec
+1  A: 

2>&1 redirects the Standard Error (2) log to Standard Output (1).

Not sure what the extra & at the end does.

If you want to pipe stderr or stdout to a file, you can do that easily.

To pipe stderr to a file, change 2>&1 to 2>filepath

Where the filepath is preferrably an absolute filepath (ex: 2>/home/user/output.mpg)

Matt
+1  A: 

Every UNIX program has two output streams: stdout and stderr. For redirection purposes, the > symbol redirects stdout (file descriptor 1) to a file. The "2>" redirects stderr (file descriptor 2) to a file, represented in this case by "&1" which tells the shell to use file descriptor 1 (which is stdout). In a php context this means that both streams will be printed in your script output, which you now doubt have figured out.

The & at the end of the command tells the shell to run the job in the background. I don't know why you would want to do this in this environment.

Matt Kane
A: 

If you want to write to a file via PHP, you can also use [exec()][1] or [passthru()][2] which have options to return all of the content from a command. system() will only return the last line.

Rob