We have a long running php file that can be started and killed whenever a sys admin wants.

How do I determine how long the php file was run each time is was run?

I wasn't able to see anything in

cat /var/log/message | grep filename

The simplest solution would be to make your script capture the termination signal (SIGTERM/SIGINT) and then calculate and save (or display) the elapsed time before exiting. Unless there are errors, it is unlikely that the script will show up in any logs (especailly if you are running it from CLI). Something like the following should do:

$time_start = microtime(true);
declare(ticks = 1);

pcntl_signal(SIGTERM, "signal_handler");
pcntl_signal(SIGINT, "signal_handler");

function signal_handler($signal) {
    global $time_start;
    switch($signal) {
        case SIGTERM:
        case SIGINT:
            file_put_contents("runtime.txt", microtime(true) - $time_start);

//your script functions go here


As long as you don't terminate your script will SIGKILL, the above should work fine (e.g. if you terminate with Ctrl+C, kill, or even killall; only kill -9 will not let the script capture the signal). [Tested and working on RHEL/CentOS with PHP-5.3.8]

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.