Can't execute PHP script using PHP exec

后端 未结 6 1425
臣服心动
臣服心动 2020-11-30 03:42

I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello

6条回答
  •  星月不相逢
    2020-11-30 04:13

    What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.

    if you just want the script's output, try:

    exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
    

    however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:

    exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
    

    the above should only output the "Hello World" to the execoutput.

    Edit:

    Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:

    alias pexec='php /home/quote2bi/tmp/helloworld.php'

    then

    exec('pexec > /tmp/execoutput.txt 2>&1 &')
    

    it seems to be a problem with the way exec handles input as opposed to the shell itself.

    -John

提交回复
热议问题