1

I have a simple php script

<?php
for($i=0;$i<10;$i++){
sleep(1);
echo $i;
}

Which I run from the command line

php myfile.php &

I would expect I can do other things in the command line, even before the scripts ends.
I do get a prompt, but when I press the enter I get [1]+ Stopped php myfile.php

2 Answers 2

2

This is expected if your terminal has the tostop option enabled. When that is set, any background process that tries to output to the terminal receives the SIGTTOU signal, and that signal's default handler stops the process. (This is done so that output from background processes doesn't mess up your terminal.)

You can disable this feature by running stty -tostop, in the same terminal.

Your current situation:

$ stty tostop
$ php ./t.php &
[1] 6484
$ 
[1]+  Stopped                 php ./t.php
$ # no messing up of the terminal, process stopped
$ fg
php ./t.php
0^C
$ 

Disable that feature:

$ stty -tostop
$ php ./t.php &
[1] 6481
$ # 01an2d 3now 45 6it m7esses 8up 9my term!
8
  • did the stty -tostop (with and without sudo) still not workin, same message Commented Jan 4, 2012 at 19:40
  • this needs to be in the same terminal you run php from, just as in the examples I posted. Commented Jan 4, 2012 at 19:42
  • It is in the same terminal. It seems like an issue of Ubuntu. If I do the & from inside a php file it works fine. Commented Jan 4, 2012 at 20:00
  • What do you mean by "do the & from inside the php file"? Commented Jan 4, 2012 at 20:01
  • inside the php I do [backtik]php t.php>log &[backtik] This, in php, runs the command inside the backtiks at the shell level (Did I say it right?). If I do that, the script runs async i.e. I can use the command line and the t.php runs in the background and outputs into log Commented Jan 5, 2012 at 1:17
0

There's probably a better solution, but for me, on Ubuntu, it works if you run it in the foreground and suspend it with Ctrl-Z, and then background it with bg:

$ php myfile.php 
^Z
[4]+  Stopped                 php myfile.php
$ bg
[4]+ php myfile.php &
$ 2#3 it4 mess5es up 6my 7term8in9al too!
[4]   Done                    php myfile.php
2
  • which Ubuntu version are you using? Commented Jan 5, 2012 at 13:46
  • Ubuntu 11.04 -- Commented Jan 5, 2012 at 23:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.