Why "php x.php &" leads to a STOPPED background process

2

x.php:

<?php
sleep(15);
?>

console:

$ php x.php &
[1] 5742
[1]+  Stopped                 x.php

Result: a "STOPPED" process - it's there forever until I send a TERM and then CONT signal.

What I need instead: the process to finish and quit after 15 seconds, so I don't see it in top or htop.

System: Ubuntu 12.04 LTS in VirtualBox; php-fpm

Meglio

Posted 2014-01-08T18:34:24.947

Reputation: 121

Actually it is not about "how to code something", but about "how to run my script in background", which looks more like a question related to "power users", as stated in the website's Tour. Re: what you are trying to do - that's described in the "What I need instead" paragraph. – Meglio – 2014-01-08T19:09:59.240

1Is there more to X.php, or did you just forget to close your PHP tag in the example? – Ƭᴇcʜιᴇ007 – 2014-01-08T19:19:43.997

Nothing else in x.php file (it's allowed and recommended to omit closing tag). Fixed my question to avoid confusion. – Meglio – 2014-01-08T19:40:49.360

Why would you want to do this? It would be easier to just run sleep 15 instead of php. – Kevin Panko – 2014-01-08T22:31:04.130

@KevinPanko The PHP script is just a minimal example used for simplified testing and posting to this website - it was used in replacement of the real workhorse-script. – Meglio – 2014-01-09T01:44:35.817

@KevinPanko , questioning why I use PHP instead of something else doesn't help with my original question. Instead it just changes direction of the discussion, unfortunately. – Meglio – 2014-01-09T01:48:28.520

There may be a simpler/better way to do what you want to do. http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem

– Kevin Panko – 2014-01-09T01:52:56.787

Answers

4

Using strace I see that the program is getting a signal SIGTTOU which means the process was in the background and attempted to write to the terminal, or in this case, change one of its modes.

ioctl(0, SNDCTL_TMR_STOP or SNDRV_TIMER_IOCTL_GINFO or TCSETSW, {B38400 opost isig icanon echo ...}) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
--- SIGTTOU {si_signo=SIGTTOU, si_code=SI_KERNEL} ---
--- stopped by SIGTTOU ---

To get around this, set the input to null:

$ php x.php < /dev/null &

The question is the same as this one: PHP script won't run in the background

Kevin Panko

Posted 2014-01-08T18:34:24.947

Reputation: 6 339