本文整理汇总了PHP中proc_terminate函数的典型用法代码示例。如果您正苦于以下问题:PHP proc_terminate函数的具体用法?PHP proc_terminate怎么用?PHP proc_terminate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了proc_terminate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: exec
/**
*
* Exec the command and return code
*
* @param string $cmd
* @param string $stdout
* @param string $stderr
* @param int $timeout
* @return int|null
*/
public static function exec($cmd, &$stdout, &$stderr, $timeout = 3600)
{
if ($timeout <= 0) {
$timeout = 3600;
}
$descriptors = array(1 => array("pipe", "w"), 2 => array("pipe", "w"));
$stdout = $stderr = $status = null;
$process = proc_open($cmd, $descriptors, $pipes);
$time_end = time() + $timeout;
if (is_resource($process)) {
do {
$time_left = $time_end - time();
$read = array($pipes[1]);
stream_select($read, $null, $null, $time_left, NULL);
$stdout .= fread($pipes[1], 2048);
} while (!feof($pipes[1]) && $time_left > 0);
fclose($pipes[1]);
if ($time_left <= 0) {
proc_terminate($process);
$stderr = 'process terminated for timeout.';
return -1;
}
while (!feof($pipes[2])) {
$stderr .= fread($pipes[2], 2048);
}
fclose($pipes[2]);
$status = proc_close($process);
}
return $status;
}
示例2: execute
function execute($cmd)
{
$descriptors = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
$stdout = '';
$proc = proc_open(escapeshellcmd($cmd), $descriptors, $pipes);
if (!is_resource($proc)) {
throw new \Exception('Could not execute process');
}
// Set the stdout stream to none-blocking.
stream_set_blocking($pipes[1], 0);
$timeout = 3000;
// miliseconds.
$forceKill = true;
while ($timeout > 0) {
$start = round(microtime(true) * 1000);
// Wait until we have output or the timer expired.
$status = proc_get_status($proc);
$read = array($pipes[1]);
stream_select($read, $other, $other, 0, $timeout);
$stdout .= stream_get_contents($pipes[1]);
if (!$status['running']) {
// Break from this loop if the process exited before the timeout.
$forceKill = false;
break;
}
// Subtract the number of microseconds that we waited.
$timeout -= round(microtime(true) * 1000) - $start;
}
if ($forceKill == true) {
proc_terminate($proc, 9);
}
return $stdout;
}
示例3: ExecWaitTimeout
/**
* Execute a command and kill it if the timeout limit fired to prevent long php execution
*
* @see http://stackoverflow.com/questions/2603912/php-set-timeout-for-script-with-system-call-set-time-limit-not-working
*
* @param string $cmd Command to exec (you should use 2>&1 at the end to pipe all output)
* @param integer $timeout
* @return string Returns command output
*/
function ExecWaitTimeout($cmd, $timeout = 5)
{
echo $cmd . "\n";
$descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
$pipes = array();
$timeout += time();
$process = proc_open($cmd, $descriptorspec, $pipes);
if (!is_resource($process)) {
throw new Exception("proc_open failed on: " . $cmd);
}
$output = '';
do {
$timeleft = $timeout - time();
$read = array($pipes[1]);
// if($timeleft > 0)
stream_select($read, $write = NULL, $exeptions = NULL, $timeleft, NULL);
if (!empty($read)) {
$output .= fread($pipes[1], 8192);
}
} while (!feof($pipes[1]) && $timeleft > 0);
if ($timeleft <= 0) {
proc_terminate($process);
throw new Exception("command timeout on: " . $cmd);
} else {
return $output;
}
}
示例4: disposeWorker
protected function disposeWorker() : \Generator
{
try {
$this->executor->cancel(new PoolShutdownException('Pool shut down'));
yield from $this->transmitter->send('', SocketTransmitter::TYPE_EXIT);
list($type) = (yield from $this->transmitter->receive());
} catch (\Throwable $e) {
// Cannot do anything about this...
}
$this->transmitter = null;
try {
$this->socket->close();
} finally {
$this->socket = null;
if ($this->process !== null) {
try {
if (empty($type) || $type !== SocketTransmitter::TYPE_EXIT) {
$status = @\proc_get_status($this->process);
if (!empty($status['running']) && empty($status['stopped'])) {
@\proc_terminate($this->process);
}
}
} finally {
@\proc_close($this->process);
$this->process = null;
}
}
}
}
示例5: proc_exec
function proc_exec($cmd)
{
$descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
$ptr = proc_open($cmd, $descriptorspec, $pipes, NULL, $_ENV);
if (!is_resource($ptr)) {
return false;
}
while (!feof($ptr)) {
$buffer = fgets($ptr);
$buffer = trim(htmlspecialchars($buffer));
echo "data: " . $buffer . "<br />";
echo "data: " . str_pad('', 4096);
ob_flush();
flush();
}
while (($buffer = fgets($pipes[FD_READ], BUF_SIZ)) != NULL || ($errbuf = fgets($pipes[FD_ERR], BUF_SIZ)) != NULL) {
if (!isset($flag)) {
$pstatus = proc_get_status($ptr);
$first_exitcode = $pstatus["exitcode"];
$flag = true;
}
if (strlen($buffer)) {
echo "data: INFO: have";
}
//. $buffer;
ob_flush();
flush();
if (strlen($errbuf)) {
ob_flush();
}
flush();
echo "data: ERR: err ";
// . $errbuf;
}
foreach ($pipes as $pipe) {
fclose($pipe);
}
/* Get the expected *exit* code to return the value */
$pstatus = proc_get_status($ptr);
if (!strlen($pstatus["exitcode"]) || $pstatus["running"]) {
/* we can trust the retval of proc_close() */
if ($pstatus["running"]) {
proc_terminate($ptr);
}
$ret = proc_close($ptr);
} else {
if (($first_exitcode + 256) % 256 == 255 && ($pstatus["exitcode"] + 256) % 256 != 255) {
$ret = $pstatus["exitcode"];
} elseif (!strlen($first_exitcode)) {
$ret = $pstatus["exitcode"];
} elseif (($first_exitcode + 256) % 256 != 255) {
$ret = $first_exitcode;
} else {
$ret = 0;
}
/* we "deduce" an EXIT_SUCCESS ;) */
proc_close($ptr);
}
return ($ret + 256) % 256;
}
示例6: terminate
public function terminate($signal = 15)
{
$ret = proc_terminate($this->_process, $signal);
if (!$ret) {
throw new Kwf_Exception("terminate failed");
}
}
示例7: stop
public function stop()
{
if (!$this->process) {
return;
}
$status = proc_get_status($this->process);
if ($status['running']) {
fclose($this->pipes[1]);
//stdout
fclose($this->pipes[2]);
//stderr
//get the parent pid of the process we want to kill
$pPid = $status['pid'];
//use ps to get all the children of this process, and kill them
foreach (array_filter(preg_split('/\\s+/', `ps -o pid --no-heading --ppid {$pPid}`)) as $pid) {
if (is_numeric($pid)) {
posix_kill($pid, 9);
// SIGKILL signal
}
}
}
fclose($this->pipes[0]);
proc_terminate($this->process);
$this->process = NULL;
}
示例8: close
public function close()
{
@\fclose($this->stdin);
@\fclose($this->stdout);
@\proc_terminate($this->process, 15);
@\proc_close($this->process);
}
示例9: execute
function execute($cmd, $stdin = null, &$stdout, &$stderr, $timeout = false)
{
$pipes = array();
$process = proc_open($cmd, array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes);
$start = time();
$stdout = '';
$stderr = '';
if (is_resource($process)) {
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
fwrite($pipes[0], $stdin);
fclose($pipes[0]);
}
while (is_resource($process)) {
$stdout .= stream_get_contents($pipes[1]);
$stderr .= stream_get_contents($pipes[2]);
if ($timeout !== false && time() - $start > $timeout) {
proc_terminate($process, 9);
return 1;
}
$status = proc_get_status($process);
if (!$status['running']) {
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $status['exitcode'];
}
usleep(100000);
}
return 1;
}
示例10: __construct
/**
* @param WorkerBootstrapProfile $bootstrapProfile
* @param string $implementationExpression
*/
protected function __construct(WorkerBootstrapProfile $bootstrapProfile, $implementationExpression)
{
$bootstrapProfile->getOrFindPhpExecutablePathAndArguments($php, $phpArgs);
$bootstrapProfile->compileScriptWithExpression($implementationExpression, null, $scriptPath, $deleteScript);
try {
$line = array_merge([$php], $phpArgs, [$scriptPath]);
$outPath = $bootstrapProfile->getOutputPath();
$this->process = proc_open(implode(' ', array_map('escapeshellarg', $line)), [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => $outPath !== null ? ['file', $outPath, 'a'] : STDERR], $pipes);
$inputSink = Sink::fromStream($pipes[0], true);
$outputSource = Source::fromStream($pipes[1], true);
$this->channel = $bootstrapProfile->getChannelFactory()->createChannel($outputSource, $inputSink);
} catch (\Exception $e) {
if (isset($pipes[1])) {
fclose($pipes[1]);
}
if (isset($pipes[0])) {
fclose($pipes[0]);
}
if (isset($this->process)) {
proc_terminate($this->process);
proc_close($this->process);
}
if ($deleteScript) {
unlink($scriptPath);
}
throw $e;
}
}
示例11: run
public function run($argv)
{
// fetch task info
$taskId = $argv[0];
$task = $this->taskDao->find($taskId);
if ($task === false) {
$this->logger->error('Task not found.');
return;
}
// TODO update pid
$jobId = $task['job_id'];
$developTaskId = $this->jobDao->getDevelopTaskId($task['job_id']);
// open process
$taskLogsDir = Config::get('taskLogsDir');
$desc = [1 => ['file', $taskLogsDir . '/scheduler_task_' . $taskId . '.out', 'w'], 2 => ['file', $taskLogsDir . '/scheduler_task_' . $taskId . '.err', 'w']];
$process = proc_open('php ' . __DIR__ . '/launcher.php develop_run.php ' . $developTaskId, $desc, $pipes);
$this->logger->info("Run [task_id: {$taskId}, job_id: {$jobId}, develop_task_id: {$developTaskId}]");
// wait
while (!$this->taskDao->isInterrupted($taskId)) {
$processStatus = proc_get_status($process);
if (!$processStatus['running']) {
if ($processStatus['exitcode'] == 0) {
$taskStatus = SchedulerTaskDao::STATUS_SUCCESS;
} else {
$taskStatus = SchedulerTaskDao::STATUS_FAILURE;
}
$this->taskDao->updateStatus($taskId, $taskStatus);
$this->jobDao->updateTaskStatus($jobId, $taskStatus);
if ($taskStatus == SchedulerTaskDao::STATUS_SUCCESS) {
$this->jobDao->insertSignal($jobId, $this->now->format('Y-m-d'));
}
$this->logger->info('Task finished, exitcode ' . $processStatus['exitcode']);
proc_close($process);
return;
}
sleep(1);
}
$this->logger->warn('Task is interrupted, send SIGTERM.');
proc_terminate($process, SIGTERM);
// stop gracefully
$countDown = 5;
while ($countDown > 0) {
$processStatus = proc_get_status($process);
if (!$processStatus['running']) {
break;
}
sleep(1);
--$countDown;
}
if ($countDown == 0) {
$this->logger->warn('Send SIGKILL.');
proc_terminate($process, SIGKILL);
}
$this->taskDao->updateStatus($taskId, SchedulerTaskDao::STATUS_FAILURE);
$this->jobDao->updateTaskStatus($jobId, SchedulerTaskDao::STATUS_FAILURE);
$this->logger->info('Task killed.');
proc_close($process);
}
示例12: __destruct
public function __destruct()
{
$status = proc_terminate($this->_server);
if ($status) {
echo "local server stopped\n";
} else {
echo "stop local server failed";
}
}
示例13: proc_terminate
function proc_terminate($signal)
{
global $mockProcTerminateFail;
if ($mockProcTerminateFail) {
return false;
} else {
return \proc_terminate($signal);
}
}
示例14: stop
/**
* @return DriverService
*/
public function stop()
{
if ($this->process === null) {
return $this;
}
proc_terminate($this->process);
$this->process = null;
$checker = new URLChecker();
$checker->waitUntilUnAvailable(3 * 1000, $this->url . '/shutdown');
return $this;
}
示例15: shutdown
public function shutdown()
{
pclose($this->config['proc-server']);
pclose($this->config['proc-docs-server']);
pclose($this->config['proc-watcher']);
pclose($this->config['proc-logs']);
proc_terminate($this->config['proc-server']);
proc_terminate($this->config['proc-docs-server']);
proc_terminate($this->config['proc-watcher']);
proc_terminate($this->config['proc-logs']);
exit;
}