Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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:

<?php
$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);
            exit;
    }
}

//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]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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