Suppressing output from exec() calls in PHP

后端 未结 4 2119
灰色年华
灰色年华 2020-12-15 17:33

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

相关标签:
4条回答
  • 2020-12-15 17:53

    Just redirect stderr to /dev/null

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

    or to stdout

    $command = 'mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql mysql 2>&1';
    
    0 讨论(0)
  • 2020-12-15 18:02

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

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

    Hope that helps!

    0 讨论(0)
  • 2020-12-15 18:13

    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.

    Edit:

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

    $command = 'mysql_tzinfo_to_sql /usr/share/zoneinfo 2> /dev/null | mysql mysql';
    exec($command, $output, $result);
    
    0 讨论(0)
  • 2020-12-15 18:13

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

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

    0 讨论(0)
提交回复
热议问题