在 PHP 中抑制 exec() 调用的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1606943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:15:34  来源:igfitidea点击:

Suppressing output from exec() calls in PHP

php

提问by JamesArmes

I have a number of command line scripts in PHP that use exec()to perform tasks such as restarting services, loading MySQL timezone files, etc. While exec() itself does not output anything to the screen, some of the commands I am running are forcing output that I can't seem to suppress (even with ob_start()/ob_end_clean()). For example, the following would load timezone files into MySQL. We run this periodically to make sure MySQLs timezone data is up to date:

我在 PHP 中有许多命令行脚本,它们使用exec()来执行诸如重新启动服务、加载 MySQL 时区文件等任务。虽然 exec() 本身不会向屏幕输出任何内容,但我正在运行的一些命令正在强制输出我似乎无法抑制的输出(即使使用 ob_start()/ob_end_clean())。例如,以下内容会将时区文件加载到 MySQL 中。我们定期运行它以确保 MySQL 的时区数据是最新的:

 $command = 'mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql';
 exec($command, $output, $result);

In this example, I would expect all output from the command to be written into $output, but I still get the following output forced to the screen:

在此示例中,我希望命令的所有输出都写入 $output 中,但我仍然将以下输出强制显示在屏幕上:

Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh87' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh88' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/Asia/Riyadh89' as time zone. Skipping it.
...

Is there any way to suppress this output? Redirecting to /dev/null is not ideal as that would cause PHP to continue processing without waiting for the command to complete.

有没有办法抑制这个输出?重定向到 /dev/null 并不理想,因为这会导致 PHP 在不等待命令完成的情况下继续处理。

Thanks in advance,
~ JamesArmes

提前致谢,
~ JamesArmes

回答by Yannick Motton

Redirecting stderr alone should not influence where processing takes place, just make sure not to add an &. It should only run in the background if you redirect the output and make it run in the background.

单独重定向 stderr 不应影响处理发生的位置,只需确保不要添加&. 如果您重定向输出并使其在后台运行,它应该只在后台运行。

Edit:

编辑:

Cracked open cygwin, you need to redirect stderr for the first command, give this a try:

破解打开cygwin,你需要为第一个命令重定向stderr,试试这个:

$command = 'mysql_tzinfo_to_sql /usr/share/zoneinfo 2> /dev/null | mysql mysql';
exec($command, $output, $result);

回答by jitter

Just redirect stderrto /dev/null

只需重定向stderr/dev/null

$command = 'mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql 2>/dev/null';

or to stdout

或者 stdout

$command = 'mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql 2>&1';

回答by Ben James

Redirecting to /dev/null does notcause PHP to stop waiting for the command. Adding a &doesdo this, you probably associate the two because the final &is often used in conjunction with a redirect.

重定向到/ dev / null的不不会导致PHP停止等待命令。添加 a&确实可以做到这一点,您可能会将两者联系起来,因为 final&通常与重定向结合使用。

In response to Yannick's (deleted) comment: it seems if you want to run something in the background in PHP, you mustredirect as well as using &. This does not mean that redirection alone causes it to run in the background.

回应 Yannick 的(已删除)评论:似乎如果您想在 PHP 中在后台运行某些东西,您必须重定向以及使用&. 这并不意味着重定向本身会导致它在后台运行。

回答by Alt-Rock Ninja Cowgirl

According to http://us3.php.net/manual/en/function.shell-exec.phpyou can assign this command's output to a variable.

根据http://us3.php.net/manual/en/function.shell-exec.php,您可以将此命令的输出分配给变量。

You don't necessarily have to do anything with the variable, which means you are effectively suppressing output.

您不必对变量执行任何操作,这意味着您可以有效地抑制输出。

Hope that helps!

希望有帮助!