简体   繁体   English

将php系统命令输出转储到文件

[英]Dump php system command output to a file

In the php system command we use the following 在php系统命令中,我们使用以下内容

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 ? '2>&1&'代表什么? I want to dump the process details to a file how do I do that ? 我想将进程详细信息转储到文件中,我该怎么做?

Thank you very much. 非常感谢你。

2>&1 redirects « stderr » to « stdout » , & at the end makes the command run in background . 2>&1重定向« stderr »« stdout »&在端部使得在命令运行背景

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 (eg reaching time limit). 没有将« stdout »重定向到从system()在后台运行的文件(或/dev/null )没有多大意义,因为一旦PHP终止(例如达到时间限制),你的命令就会被杀死。

From system() manual: 来自system()手册:

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. 如果不这样做将导致PHP挂起,直到程序执行结束。

2>&1 redirects the Standard Error (2) log to Standard Output (1). 2>&1将标准错误(2)日志重定向到标准输出(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. 如果要将stderr或stdout传递给文件,可以轻松完成。

To pipe stderr to a file, change 2>&1 to 2>filepath 要将stderr管道传输到文件,请将2>&1更改为2> filepath

Where the filepath is preferrably an absolute filepath (ex: 2>/home/user/output.mpg) 文件路径最好是绝对文件路径(例如:2> /home/user/output.mpg)

Every UNIX program has two output streams: stdout and stderr. 每个UNIX程序都有两个输出流:stdout和stderr。 For redirection purposes, the > symbol redirects stdout (file descriptor 1) to a file. 出于重定向的目的,>符号将stdout(文件描述符1)重定向到文件。 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). “2>”将stderr(文件描述符2)重定向到一个文件,在本例中用“&1”表示,它告诉shell使用文件描述符1(stdout)。 In a php context this means that both streams will be printed in your script output, which you now doubt have figured out. 在php上下文中,这意味着两个流都将打印在您的脚本输出中,您现在怀疑它已经找到了。

The & at the end of the command tells the shell to run the job in the background. 命令末尾的&告诉shell在后台运行作业。 I don't know why you would want to do this in this environment. 我不知道你为什么要在这种环境中这样做。

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. 如果要通过PHP写入文件,还可以使用[exec()][1][passthru()][2] ,它们具有从命令返回所有内容的选项。 system() will only return the last line. system()只返回最后一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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