Wpis z mikrobloga

# BUILD PATHS
$script = arraypop(explode('/',$SCRIPTNAME)) ;
$scriptdir = substr($SCRIPTNAME,0,strlen($SCRIPTNAME)-strlen($script)) ;
$scriptURL = 'http://'. $HTTP
HOST . $scriptdir . "$script" ;
$curlURL = 'http://'. $HTTP
HOST . $scriptdir . "$script?runscript=curl" ;

# Indicate that script is being called by CLI
if ( php
sapiname() == 'cli' ) {
$CLI = true ;
}

# Action if script is being called by cURL
prompt()
if ( $runscript == 'curl' ) {
$cmd = "/usr/local/bin/php ".$PATHTRANSLATED ; // server location of script to run
exec($cmd) ;
exit;
}

# USER INTERFACE
// User answer after submission.
if ( $post ) {
cURL
prompt($curlURL) ;
echo 'Background CLI';
echo 'O.K. If all goes well, '.$script.' is working hard in the background with no ' ;
echo 'timeout limitations.

' ;
echo '' ;
exit ;
}
// Start screen.
if ( !$CLI AND !$runscript ) {
echo 'Background CLI' ;
echo '' ;
echo 'Click to run '.$script.' from the PHP CLI command line, in the background.

' ;
echo '' ;
echo '' ;
exit ;
}

# cURL URL PROMPT FUNCTION
function cURLprompt($urlpath) {
obstart(); // start output buffer
$c=curl
init($urlpath);
curl
setopt($c, CURLOPTTIMEOUT, 2); // drop connection after 2 seconds
curl
exec($c);
curlclose($c);
ob
endclean(); // discard output buffer
}
?>
up
down
5 kexianbin at diyism dot com ¶3 years ago
Both set
timelimit(...) and iniset('maxexecutiontime',...); won't count the time cost of sleep,filegetcontents,shellexec,mysqlquery etc, so i build this function mybackgroundexec(), to run static method/function in background/detached process and time is out kill it:

myexec.php:
php
function my
backgroundexec($functionname, $params, $strrequires, $timeout=600)
{$map=array('"'='\"', '$'=>'\$', '`'=>'`', '\'=>'\', '!'=>'!');
$str
requires=strtr($strrequires, $map);
$path
run=dirname($SERVER['SCRIPTFILENAME']);
$mytargetexec="/usr/bin/php -r \"chdir('{$pathrun}');{$strrequires} \$params=jsondecode(filegetcontents('php://stdin'),true);calluserfuncarray('{$functionname}', \$params);\"";
$my
targetexec=strtr(strtr($mytargetexec, $map), $map);
$my
backgroundexec="(/usr/bin/php -r \"chdir('{$pathrun}');{$strrequires} mytimeoutexec(\"{$mytargetexec}\", filegetcontents('php://stdin'), {$timeout});\" <&3 &) 3<&0";//php by default use "sh", and "sh" don't support "<&0"
my
timeoutexec($mybackgroundexec, jsonencode($params), 2);
}

function mytimeoutexec($cmd, $stdin='', $timeout)
{$start=time();
$stdout='';
$stderr='';
//fileputcontents('debug.txt', time().':cmd:'.$cmd."\n", FILEAPPEND);
//file
putcontents('debug.txt', time().':stdin:'.$stdin."\n", FILEAPPEND);

$process=procopen($cmd, [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes);
if (!is
resource($process))
{return array('return'=>'1', 'stdout'=>$stdout, 'stderr'=>$stderr);
}
$status=procgetstatus($process);
posixsetpgid($status['pid'], $status['pid']); //seperate pgid(process group id) from parent's pgid

stream
setblocking($pipes[0], 0);
stream
setblocking($pipes[1], 0);
stream
setblocking($pipes[2], 0);
fwrite($pipes[0], $stdin);
fclose($pipes[0]);

while (1)
{$stdout.=stream
getcontents($pipes[1]);
$stderr.=stream
getcontents($pipes[2]);

if (time()-$start>$timeout)
{//proc
terminate($process, 9); //only terminate subprocess, won't terminate sub-subprocess
posixkill(-$status['pid'], 9); //sends SIGKILL to all processes inside group(negative means GPID, all subprocesses share the top process group, except nested mytimeoutexec)
//file
putcontents('debug.txt', time().":kill group {$status['pid']}\n", FILEAPPEND);