/
systemExecute.php
66 lines (52 loc) · 1.55 KB
/
systemExecute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
// Execute a system command with timeout control.
// $command - System command.
// [opt]$timeout - Timeout in seconds (default 1 minute).
// [opt]$sleep - Sleep cycle interval in milliseconds (default 2 ms).
// Sleep is used to wait for the command to finish.
// Return value:
// 0 Success.
// 1 Command error.
// 2 Timeout error.
function systemExecute($command, $timeout = 60, $sleep = 2)
{
// Execute $command in a new process
$pid = PsExec($command);
if( $pid === false )
return 1;
$cur = 0;
// Second, loop for $timeout seconds checking if process is running
while( $cur < $timeout )
{
sleep($sleep);
$cur += $sleep;
// If process is no longer running, return true;
if( !PsExists($pid) )
return 0; // Process must have exited, success!
}
// If process is still running after timeout, kill the process and return false
PsKill($pid);
return 2;
}
function PsExec($commandJob) {
$command = $commandJob.' > /dev/null 2>&1 & echo $!';
exec($command ,$op);
$pid = (int)$op[0];
if($pid!="") return $pid;
return false;
}
function PsExists($pid) {
exec("ps ax | grep $pid 2>&1", $output);
while( list(,$row) = each($output) ) {
$row_array = explode(" ", $row);
$check_pid = $row_array[0];
if($pid == $check_pid) {
return true;
}
}
return false;
}
function PsKill($pid) {
exec("kill -9 $pid", $output);
}
?>