2

I was just wondering if it's possible to detect the current execution timing for a running script, I am creating an application to ping some computers on the network. As this is being done from a Linux machine the pinging system differs from Windows.

On a linux machine, if the computer is off then the server will hang on the primary message after issuing the ping command and not have any more output.. Will just hang (with my experience with linux pinging)

So I have this script:

$Computer_Array = array( 
  "Managers" => "192.168.0.5",
  "Domain Controller" => "192.168.0.1"
  "Proxy Controller" => "192.168.0.214"
);
foreach ($Computer_Array AS $Addresses){ 
  exec('ping'.$Addresses, $Output);
}

Later on this will be used to display statistics.. Now the problem is, as the managers computer is subject to both power conditions such as on or off when issuing the ping command, just hangs.. So i'm wondering if there is a method to capture the microtime(); of the current executing function, if it exceeds a threshold then move on to the next element. I would rather keep this to core PHP, but if such solution can only be done via AJAX or another language, then I would have to consult the developer if it's alright to integrate an external method.

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
  • This is probably best achieved running a process in the background, which you could easily check on using `microtime` to see how long it has been, and continuing to the next client if it times out. Check out http://stackoverflow.com/questions/45953/php-execute-a-background-process#45966 – Mahdi.Montgomery May 16 '13 at 23:52
  • 1
    why not just set a timeout in the ping ( [ -T timestamp option ]) command line? –  May 17 '13 at 00:00

2 Answers2

1

The ping command allows you to specify how long it will wait before giving up:

ping -c 5 -t 1 127.0.0.2

This will return after one second, regardless of how many pings have been sent. The exact command line arguments would vary between platforms.

Alternatively, if you can use pcntl, look into pcntl_alarm(); it will deliver a SIGALRM signal to your application after a certain amount of time that can be caught.

Lastly, and I haven't tested this myself, you could try using proc_open() and use stream_select() on one of the pipes; if nothing has happened on the pipe after a certain time you can then kill off the process.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

If you want to do this with PHP, or run into a similar issue, here's an example using code from php execute a background process

The PHP script would need write permissions to the output files. This concept would essentially work for anything, from a ping to another PHP script.

function isRunning($pid){
    try{
        $result = shell_exec(sprintf("ps %d", $pid));
        if( count(preg_split("/\n/", $result)) > 2){
            return true;
        }
    }catch(Exception $e){}

    return false;
}

$cmd = "ping 127.0.0.1";
$outputfile = "output";
$pidfile = "pid";

$start = microtime(true);

// Don't last longer than 10 seconds
$threshold = 2;

// Ping and get pid
exec(sprintf("%s > %s 2>&1 & echo $! > %s", $cmd, $outputfile, $pidfile));
$pid = `tail -n 1 $pidfile`;

// Let the process run until you want to stop it
while (isRunning($pid)){

    // Check output here...

    if ((microtime(true)-$start) > $threshold){
        $o = `kill $pid`;
        die("Timed out.");
    }
}


$end = microtime(true);
$time = $end - $start;

echo "Finished in $time seconds\n";
Community
  • 1
  • 1
Mahdi.Montgomery
  • 2,024
  • 4
  • 17
  • 21