Thursday, April 30, 2009

Process Management in PHP

My PHP script downloads a series of binary patches and places them into a directory for further processing. It also creates lock files to prevent two or more instances of my script from being run.

The Problem

When the script receives a SIGINT (Ctrl+C) or SIGTERM signal from the OS, my script is prevented from cleaning up the temporary files, which usually occupies hundreds of MB of space. The lock files also become stale, so my script prevents itself from being run until the next reboot, when the /tmp directory is cleaned up.

The Solution

The solution is to catch the signals so that my script is given one last chance to perform cleanup:
function sig_handler($signo)
{
switch ($signo) {
case SIGTERM:
case SIGQUIT:
case SIGABRT:
case SIGINT:
echo "==> Software Update has been aborted.\n";
echo "==> Performing cleanup...\n";
cleanup();
exit();

break;
default:
// Unknown signal. Do something here...
exit();
}

}

pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGQUIT, "sig_handler");
pcntl_signal(SIGABRT, "sig_handler");
pcntl_signal(SIGINT, "sig_handler");

Wednesday, April 29, 2009

tail -F in PHP

I'm currently writing a PHP script that pushes live log data to the front-end UI. I'm using a streaming Comet model such that my back-end would update the front-end's screen every time the log file is updated.

Unix has a nifty utility called 'tail' that can continuously monitor a file for updates and display them to standard output (which is the usually the monitor). By capturing tail's output via popen(), I was able to make a log file viewer with just a few lines:

exec("/usr/bin/killall tail");
// execute myapp in the background and redirect all of its output to logfile
exec("/usr/local/bin/myapp > /var/log/mylogfile.log 2>&1 &");

$handle = popen("/usr/bin/tail -F /var/log/mylogfile.log 2>&1", 'r');
while(1) {
$buffer = fgets($handle);

if (trim($buffer) == '-- ok') { // this string signifies end of execution
pclose($handle);
exec("/usr/bin/killall tail");
exit();
} else {
echo trim($buffer);
// if you're using Comet, push output to the stream here
}

ob_flush();
flush();
}
Very cool...