Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    • Archives

    • Recent comments

    Executing and checking background shell process from PHP

    23rd May 2007

    Found a nicely illustrated method for running a background shell command from PHP and continuously checking if the process is still running.

    Here’s sample code without explanations:

    1. function run_in_background($Command, $Priority = 0)
    2. {
    3.  if($Priority)
    4.   $PID = shell_exec("nohup nice -n $Priority $Command 2> /dev/null & echo $!");
    5.  else
    6.   $PID = shell_exec("nohup $Command 2> /dev/null & echo $!");
    7.  return($PID);
    8. }
    10. function is_process_running($PID)
    11. {
    12.  exec("ps $PID", $ProcessState);
    13.  return(count($ProcessState) >= 2);
    14. }

    To run something like hmmsearch from the HMMER package, you’d do this:

    1. echo("Running hmmsearch. . .")
    2. $ps = run_in_background("hmmsearch $hmmfile $fastafile > $outfile");
    3. while(is_process_running($ps))
    4. {
    5.  echo(" . ");
    6.  ob_flush();flush();
    7.  sleep(1);
    8. }

    8 Responses to “Executing and checking background shell process from PHP”

    1. Joe Says:

      function is_process_running($PID)
      exec(“ps $PID”, $ProcessState);
      return(count($ProcessState)>= 2);

      IMO, it is more elegant to check for the exit code instead.

    2. Bogdan Says:


      how would you do that, given the example application above? I see no way of getting exit status from a process which has finished.

    3. Alex Says:

      I am new to php, can anyone please write a piece of code for me to initialize “$hmmfile $fastafile & $outfile” to execute command $ps = run_in_background(“hmmsearch $hmmfile $fastafile > $outfile”); as these are file handlers, not simple variables.


    4. David Wilson Says:

      How can you kill a process triggered from PHP (example: PHP runs BASH, and then BASH runs py script). I normally do kill -TERM -PID so I kill the parent and all the children processes.

      But it does not work when I use PHP to start everyting. I get: bash: kill: (-3707) - No such process

      However, ps command tells me that the process is running.

      Anyone knows what I am doing wrong? Hell really appreciated.


    5. Bogdan Says:


      I was only running processes which would sooner or later exit/terminate on their own – no daemon/background processes.
      So I never had to kill a process from PHP.

      If you are wrapping the final script into a bash script, then you could add some kind of process management to that bash script.
      For example, you could get the PID of the background python script, `sleep` for some time, then kill it from the bash script, then terminate the script.

      I would generally advise against any kind of background process management from PHP.
      Do you really need it?
      It would likely be better to have an independent server-side “worker” process, connected to a job queue (e.g. `beanstalkd`), and have your PHP/frontend simply add jobs to that queue.

    6. daianne Says:

      Hi, using this how would i catch the error if the command i execute has failed?

    7. Bogdan Says:

      one of the ways would be to redirect stdout and stderr to files, and then check those files for any messages about success or failure – or even check if stdout/stderr/output files are larger than zero bytes (because zero may indicate an early failure, if the process is not running anymore).

      Alternatively/in addition, if you start a shell script wrapper around your target application, then you can collect (from the shell script) the exit code of your target application and write it to some file, and then check if exit code was 0 or not.

    8. daianne Says:

      Thank you very much for your response and advice. I already did that, but so far the only one that works for me is to write the message on a file. However, i don’t know how should i detect the error on that file, because if i’m going to search the word ‘Error or ‘Failed’, it will take forever since my data testing is more than 30gb. And i can’t get to work out the thing that you said to check the file size since the log also writes the process on the file created. So even if I check the file size, it wont help me because it will always be greater than zero.

      as for the alternative that you mention I can’t get the exit code. Or better yet I’m not sure how to get it done.

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>