PHP script won't run in the background

2019-02-24 12:21发布

问题:

I am trying to run a php CLI script in the background and it just won't run - it has a status of Stopped SIGTOU (Trying to write output) - Here are the details

  • Mac OS X Lion 10.7.2
  • PHP 5.3.6 with Suhosin-Patch (cli) (built: Sep 8 2011 19:34:00)

I created a basic script test.php

<?php echo 'Hello world'.PHP_EOL; ?>

Here are the results of various tests:-

  • php -f test.php (Hello world gets displayed)
  • php -f test.php >test.log 2>&1 (Hello world gets put into test.log)
  • php -f test.php >test.log 2>&1 & --- I get [1]+ Stopped(SIGTTOU) php -f test.php > test.log 2>&1 -- and the job just sits there doing nothing nothing gets logged however lsof shows the log file is open

It is something to do with PHP? A similar shell script gets executed no problems in the background.

回答1:

If readline is enabled in your build of php, simply pass /dev/null as the input.

In your example above, it would be:

php -f test.php </dev/null >test.log 2>&1


回答2:

This is resolved now -- thanks to all who responded. The problem was that Apple provide PHP pre-built with the OS - the CLI version was built with readline included - http://www.php.net/manual/en/intro.readline.php ... this prevents any background running of scripts because readline automatically starts IO with the TTY ...

My problem was that I couldn't build my own version of PHP because of this -> http://forums.macrumors.com/showthread.php?t=1284479 - once I got that resolved my background script issue was gone :)



回答3:

Well, a PHP script stops when its done execution, ie a simple echo "Hello World" is done execution as soon as it outputted the string, i would guess it have something to do with it ;-)