Monitor Script Execution with PHP

The following is a very simple script that can be used as a framework to monitor the execution of a script.

Comments are used for explanation.

<?php
/* Ensure the viewer has logged in. */
session_start();
if (!isset($_SESSION['bLogged_in']))
	die();
?>
<html>
<head>
<?php
        /* 
        The escapeshellarg is there as a reminder to escape any input.  In this
        case, it is unnecessary, but if $_GET was used, it would be important.
        */
	$script=escapeshellarg('httpd');

        /* 
        Use ps and grep to check for the script running.  ps -C $script may also be used.
        */
	$ps=nl2br(`ps|grep $script`);

        /* 
        This command is used to simulate execution of a script.  It just appends the date
        to a file.  
        */
	$time_data_maker=`date >> ../test.data`;

        /* 
        Test to see if the command is executing
        */
	if ($ps != '')
        {
                /* Update the timestamp on the session file */
                $_SESSION['tick']=1;
                /* Tell the browser to refresh the page in 3 seconds */
		echo '<META HTTP-EQUIV="Refresh" CONTENT="3" />'."\n";
        }
?>
<title><?php $_GET['title']?></title>
<style>
body 
{ 
	font-family : "Courier New", Courier, monospace;
	font-size: .8em; 
}
</style>
</head>
<body>
<?php
/* If script is not running ... */
if ($ps == '')
{
	echo $script.'is not running<br />';
}
else
{
	echo $script.'is running<br />Current Status<br />';
        /* Display contents of file */
	echo nl2br(file_get_contents('../test.data'));
}
?>
</body>
</html>

Notes:

  • Refreshes stop when script stops
  • Page display prolongs session until it finishes
  • Session id may be used to identify data file, script name can make a nice extension
  • Placed in an iframe, with a print button, this is a very nice interface