Linux Background PHP process

linuxPHP

Ok, this seems really weird. When I am running a php script from the command line with & at the end to run it in the background, it's immediately stopped. I tried it on another server and it worked as expected; the job is running in the background.

The PHP

#!/usr/bin/php
<?php
sleep (5);

Output on Server 1

[mk@li89-44 html]# ./test.php &
[1] 4938
[mk@li89-44 html]# jobs
[1]+  Running        ./test.php &

Output on Server 2

[mk@dev html]# ./test.php &
[1] 4938
[mk@dev html]# jobs
[1]+  Stopped        ./test.php &

On Server 2, I can get it in the background like so:

[mk@dev html]$ ./test.php
ctrl + z
[1]+  Stopped                 ./test.php
[mk@dev html]$ bg
[1]+ ./test.php &
[mk@dev html]$ jobs
[1]+  Running                 ./test.php

Also on Server 2, if i do something like wget "http://fileserver.com/largefile.gz" & it goes into the bg running as expected. Running nohup ./test.php & on server 2 also works as expected.

Both servers are running centos, different versions, and different versions of php as well. I dunno if that's relevant, or if this can be explained just by the way jobs, fg, bg work on linux.

Best Answer

If you look at an strace of the process, you can see that PHP always initializes the terminal, which requires waiting for PHP to control the terminal:

ioctl(0, TCSETSW, {B38400 opost isig icanon echo ...}) = ? ERESTARTSYS
--- {si_signo=SIGTTOU, si_code=SI_KERNEL} (Stopped (tty output)) ---
--- Stopped (tty output) by SIGTTOU ---

A simple workaround:

./test.php > /dev/null &

Related Topic