I need to run a unix shell script (my_script
) in background and monitor it from web interface.
I start the actual script from php like so:
$command = 'my_script';
$pid= exec("nohup {$command} > {$logFile} & echo $!");
Then $pid
contains the pid of my process and I can find out if the process is still running by checking file_exists("/proc/{$pid}")
.
However, here's the problem: how do I find out when the process has terminated and how long it ran when it is terminated?
Also, is there a good way to find out when the process has started? I can record the timestamp in PHP before or after I make the exec
call, but that will be somewhat inaccurate.
EDIT:
To clarify, PHP script can't wait for shell script to finish. The shell script needs to be run in the background.
I can probably wrap three commands (record time started, run my_script
, record time finished) into one shell with sh -c
pass it to nohup
, but I would prefer the whole sequence to be atomic, so that I do not end up with only start time recorded if something goes wrong.