Skip to content

June 22, 2009

2

PHP/JSON: A working example – log update on a background process (Part 2/2)

by Joe Kuan

phpPHP side (Server side)

On the server side, here is the code for the start_proc.php which starts a background process

<?php
require_once("./common.php");
$bgpid = $_GET['bgpid'];

/* Launch background process */
if ($bgpid) {
   /* Make sure the prev proc has been finished. If so, start a new one */
   if (!is_proc_running($bgpid)) {
      $rc = start_proc("/tmp/bgproc.sh", $bgpid, $errmsg);
      $result['bgpid'] = $bgpid;
   } else {
      $rc = 1;
      $errmsg = "Background job is already running";
   }
} else {
   $rc = start_proc("/tmp/bgproc.sh", $bgpid, $errmsg);
}

$result['Status'] = $rc;
$result['bgpid'] = $bgpid;
$result['Message'] = $errmsg;
echo json_encode($result);
?>

In common.php, it defines common use functions.

<?php
function is_proc_running($pid) {
   exec("ps -p {$pid} > /dev/null 2>&amp;1", $out, $rc);
   if (!$rc)
     return true;
   return false;
}

function start_proc($prog_name, &amp;$pid, &amp;$errmsg) {
  exec("bash -c '{$prog_name} >> /tmp/log_messages 2>&amp;1 &amp; echo $!'", $out, $rc);
  if (!$rc) {
     $pid = intval(trim($out[0]));
     $errmsg = "Process has been started {$pid}";
  } else {
     foreach($out as $o)
        $errmsg .= $o;
  }
  return $rc;
}
?>

The get_log_messages.php simply retrieves the content of a log file.

<?php
if (file_exists("/tmp/log_messages"))
  echo nl2br(file_get_contents("/tmp/log_messages"));
?>

The get_proc_status.php simply retrieves the status of the background process

<?php
  require_once("./common.php");
  $bgpid = $_GET['bgpid'];

  if (is_proc_running($bgpid)) {
    $result['Status'] = 0;
    $result['Running'] = 1;
    $result['Message'] = 'Process is still running';
  } else {
    $result['Status'] = 0;
    $result['Running'] = 0;
    $result['Message'] = "Process is finished. {$bgpid}";
  }
echo json_encode($result);
?>

Bash script (Server side)

Also on the server side, I have created a little shell script bgproc.sh to simulate a background process printing out log messages. The script basically appends a message to a file every second until the 100th second.

#!/bin/sh
i=0
echo '' &amp;gt; /tmp/log_messages
while (true)
do
  i=$(($i+1))
  echo &amp;quot;Log message: line $i&amp;quot; &amp;gt;&amp;gt; /tmp/log_messages
  sleep 1
  if [ $i -gt 100 ]
  then
     break
  fi
done

Test Run

When the start button is clicked, the Status box reports a process has been started. Then the Show Log button is clicked, the log messages are then gradually displayed.
Bg process started

As long as the background process is running, the status box is updated by PeriodicalUpdater (with get_proc_status.php) with the message ‘Process is still running’. When process is finished, the PeriodicalUpdater is stopped with the message ‘Process is finished’.
bg process is finished

Of course, if your webpage doesn’t require task control with just constant update, then the whole example will be a lot simpler. All you need is PeriodicalUpdater call. This is only a primitive demo to show how everything links together. There are plenty of rooms to polish up, such as progressive log update instead of a full log update, proper spawning and capturing exit code of the background process, etc.

At the time of writing, this example works on IE 8, Safari 4 and Firefox 3. Hope this helps.

Click here for Part 1.

I work for iTrinegy and here are my other technical blogs

2 Comments Post a comment
  1. duong mannh
    Oct 21 2010

    download file ????

    Reply
    • Joe Kuan
      Oct 21 2010

      You can click on one of the tools in the toolbar to copy & paste the code. Sorry that the code is not in downloadable form because not many people request for it.

      Thanks
      Joe

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: