0

I am running a simple PHP script on a CentOS 6.5 with php5.3.3

#!/usr/bin/env php
<?php
    echo "hello"."\n";
    return 0;
?>

My issue is that if I do ./testscript, it runs fine, if I do nohup ./testscript &, it runs fine, if I do ./testscript < /dev/null > /dev/null & it runs fine (it is the same without the output redirection). But if I do ./testscript &, the script is stopped (and there is no output) :

[1]+  Stopped                 ./testscript 

From what I understand, this is because php is expecting some input from STDIN. But the question is why ? As you can see, there is nothing requiring some input in this simple script, and I didn't have this behavior on my php5.4 on Debian. Does this change in behavior comes from php version or OS ? And why is it this way ? Is there a way to change that and to allow me to run script in background without having to add /dev/null as STDIN ?

Thank you !

user3017110
  • 155
  • 12
  • Try doing something like `./testscript > output.log 2>&1 &`. It should stop, but at least you can see what it output before it crapped out. – Mr. Llama Nov 06 '14 at 16:35
  • @hellcode: -1. that's how shell script MUST be constructed. no shebang, no `./foo.php`-type invocation. – Marc B Nov 06 '14 at 16:36
  • @Mr.Llama: output.log is empty this way. And the script stops because of STDIN.. – user3017110 Nov 06 '14 at 16:44
  • 1
    Related (CLI version was built with readline included): http://stackoverflow.com/questions/8194755/php-script-wont-run-in-the-background – hellcode Nov 06 '14 at 17:02
  • Related: http://stackoverflow.com/questions/17912137/command-line-script-run-in-background-goes-in-stopped-state – hellcode Nov 06 '14 at 17:07
  • @hellcode - Out of curiosity, why does the `nohup` version work but not regular `&`? Does nohup silently feed `/dev/null` as input? – Mr. Llama Nov 06 '14 at 17:30
  • @hellcode : Thanks, this is exactly what I wanted to know !! :) – user3017110 Nov 12 '14 at 11:16

1 Answers1

0

Your program does nothing but printing something and terminating with return 0 (sure you don't mean exit()?). After the code is executed the php interpreter has nothing to do any more and simply signals to the shell "Hey Buddy! I am done! You can notify the user!" Calling it with & simply tells the shell: "Run this command in background." Which it does. Exactly once.

In order to have your program run indefinitely, it has to actually run indefinitely, too.

<?php
  while(true) {
    // Doing something useful
    sleep(5);
  }
?>

With nohup, the "program" (the php interpreter, in this case) is basically prohibited to terminate - but it will do not much any more. As for the redirections - they will actually target the php interpreter, which will wait for further input, which is not coming because /dev/null does not emit anything.

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
  • 1
    He doesn't indicate that he wants his script to run forever. `nohup` doesn't prevent (or prohibit) the program from exiting, it prevents it from being killed because of a hangup signal. His question is *why* is php waiting for input. He hasn't asked it to do that. – Etan Reisner Nov 06 '14 at 16:48
  • Actually, what else should be the reason to have the input/output redirection? Maybe I simply don't get the question. – Markus W Mahlberg Nov 06 '14 at 16:51
  • He was indicating various ways he has run the program such that it worked. To illustrate the various things that solve the problem he is having... namely that his script seems to be waiting on stdin and he doesn't know why. – Etan Reisner Nov 06 '14 at 16:53
  • @Markus W Mahlberg: as Etan says, I don't want the script to run forever. And I can use return or exit or nothing, my issue is the same : the php script stops before doing anything (it doesn't echo my hello). When I say "stop", I mean the script has the status "stopped" (T in ps aux), which kind of like a pause. – user3017110 Nov 06 '14 at 16:54
  • Which I explained - when a stdin is opened for php, it waits for more things to come after the original script is run. Try it with `php << EOF`... – Markus W Mahlberg Nov 06 '14 at 16:57
  • @user3017110: Ah, now I get it. I can reproduces the stopped problem with PHP. The problem might be related to http://unix.stackexchange.com/questions/67645/why-do-background-jobs-hang-depending-on-the-size-of-the-output When I use a simple wrapper script like `php -f $@ &`, everything works as intended. – Markus W Mahlberg Nov 06 '14 at 17:34