PHP script won't run in the background

2019-02-24 12:28发布

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.

3条回答
我命由我不由天
2楼-- · 2019-02-24 12:32

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楼-- · 2019-02-24 12:44

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
查看更多
等我变得足够好
4楼-- · 2019-02-24 12:50

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 ;-)

查看更多
登录 后发表回答