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. }

    5 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.

    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>