本文整理汇总了PHP中posix_kill函数的典型用法代码示例。如果您正苦于以下问题:PHP posix_kill函数的具体用法?PHP posix_kill怎么用?PHP posix_kill使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了posix_kill函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: acquire
/**
* Creates a lock file for the given name.
*
* @param string $name The name of this lock file
* @param bool $check If we should check if the process exists in addition to check for a lock file
* @return boolean
*/
public static function acquire($name, $check = false)
{
$pid = self::getProcessID($name);
if (!empty($pid)) {
// Test asks us to check if the process is still running
if ($check) {
if (function_exists('posix_kill')) {
$exists = posix_kill($pid, 0);
} else {
$retval = 0;
$out = array();
exec('kill -s 0 ' . $pid, $out, $retval);
$exists = $retval == 0;
}
if ($exists) {
return false;
}
}
return false;
}
// create the pid file
$fp = fopen(self::getProcessFilename($name), 'w');
flock($fp, LOCK_EX);
fwrite($fp, getmypid());
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
示例2: CheckDaemon
private function CheckDaemon()
{
if ($pidfile = @fopen($this->pidfile_path, "r")) {
$pid_daemon = fgets($pidfile, 20);
fclose($pidfile);
$pid_daemon = (int) $pid_daemon;
// bad PID file, e.g. due to system malfunction while creating the file?
if ($pid_daemon <= 0) {
echo "removing bad pid_file (" . $this->pidfile_path . ")\n";
unlink($this->pidfile_path);
return false;
} elseif (posix_kill($pid_daemon, 0)) {
// yes, good bye
echo "Error: process for " . $this->pidfile_path . " is already running with pid={$pid_daemon}\n";
return false;
} else {
// no, remove pid_file
echo "process not running, removing old pid_file (" . $this->pidfile_path . ")\n";
unlink($this->pidfile_path);
return true;
}
} else {
return true;
}
}
示例3: dealMission
/**
* 多进程处理任务
* @param callback $mission_func 子进程要进行的任务函数
*/
public function dealMission($mission_func)
{
$this->_mission_func = $mission_func;
for ($i = 0; $i < $this->_process_num; $i++) {
$pid[] = pcntl_fork();
if ($pid[$i] == 0) {
//等于0时,是子进程
$this->_func_obj->{$mission_func}($i + 1);
//结束当前子进程,以防止生成僵尸进程
if (function_exists("posix_kill")) {
posix_kill(getmypid(), SIGTERM);
} else {
system('kill -9' . getmypid());
}
exit;
} else {
if ($pid > 0) {
//大于0时,是父进程,并且pid是产生的子进程的PID
//TODO 可以记录下子进程的pid,用pcntl_wait_pid()去等待程序并结束
pcntl_wait($status);
} else {
throw new Exception('fork fail');
}
}
}
}
示例4: stopDownload
public function stopDownload()
{
$wget_pid = $this->_get_wget_pid($_localImagePath);
if ($wget_pid != -1) {
posix_kill($wget_pid, SIGKILL);
}
}
示例5: killChildProcesses
/**
* Kills all running child-processes
*/
public function killChildProcesses()
{
$child_pids = $this->getChildPIDs();
for ($x = 0; $x < count($child_pids); $x++) {
posix_kill($child_pids[$x], SIGKILL);
}
}
示例6: execute
public final function execute()
{
$hasThreads = function_exists('pcntl_signal');
if (!$hasThreads || Cli::getInstance()->isSimulation()) {
flush();
try {
return $this->executeNoThread();
} catch (Interrupt $e) {
throw $e;
} catch (Exception $e) {
echo $e;
}
return;
}
pcntl_signal(SIGCHLD, SIG_IGN);
$pid = pcntl_fork();
if ($pid < 1) {
$this->_run();
posix_kill(posix_getpid(), 9);
pcntl_waitpid(posix_getpid(), $temp = 0, WNOHANG);
pcntl_wifexited($temp);
exit;
//Make sure we exit...
} else {
$this->pid = $pid;
}
}
示例7: killProcess
/**
* @param string $pidPath
* @param bool $throwException
* @param LoggerInterface $logger
* @param LOLClientInterface|null $client
*
* @throws \RuntimeException
*/
public static function killProcess($pidPath, $throwException, LoggerInterface $logger, LOLClientInterface $client = null)
{
$pid = (int) file_get_contents($pidPath);
// Test if process is still running
$output = [];
exec('ps ' . $pid, $output);
if (!isset($output[1])) {
if (null != $client) {
$logger->debug('Client ' . $client . ' (pid: #' . $pid . ') not running, deleting cache pid file');
} else {
$logger->debug('Process #' . $pid . ' not running, deleting cache pid file');
}
unlink($pidPath);
return;
}
// Kill
if (posix_kill($pid, SIGKILL)) {
if (null != $client) {
$logger->debug('Client ' . $client . ' (pid: #' . $pid . ') has been killed');
} else {
$logger->debug('Process #' . $pid . ' has been killed');
}
unlink($pidPath);
} else {
if ($throwException) {
throw new \RuntimeException('Cannot kill the process #' . $pid . ', please kill this process manually');
}
$logger->critical('Cannot kill the process #' . $pid . ', please kill this process manually');
}
}
示例8: sig_handler
function sig_handler($signo)
{
$log = getLog('queue/queued');
$log->setData("\r\n" . 'Signal Receive:' . $signo . ' ' . date('Y-m-d H:i:s'));
$pids = @file(ROOT . 'logs/queue/pids');
if (is_array($pids)) {
foreach ($pids as $pid) {
list(, , $pid) = explode('_', $pid);
if (!empty($pid)) {
$pid = intval($pid);
@posix_kill($pid, SIGTERM);
if (file_exists(ROOT . 'logs/queue/pid/' . $pid)) {
@unlink(ROOT . 'logs/queue/pid/' . $pid);
}
}
}
}
switch ($signo) {
case SIGHUP:
@popen(ROOT . 'daemon/queue/queued.php 2>&1 > /dev/null &', "r");
break;
}
$log->setData("\r\n" . 'Signal Process Finished:' . date('Y-m-d H:i:s'))->write();
exit;
}
示例9: 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;
}
示例10: stopServer
/**
* Stop the jackrabbit server. If it is not running, silently return.
*/
public function stopServer()
{
$pid = $this->getServerPid();
if ($pid) {
posix_kill($pid, SIGKILL);
}
}
示例11: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$id = $input->getArgument('id');
// Do a cleanup
$worker = new Resque\Worker('*');
$worker->cleanup();
if ($id) {
if (false === ($worker = Resque\Worker::hostWorker($id))) {
$this->log('There is no worker with id "' . $id . '".', Resque\Logger::ERROR);
return;
}
$workers = array($worker);
} else {
$workers = Resque\Worker::hostWorkers();
}
if (!count($workers)) {
$this->log('<warn>There are no workers on this host</warn>');
}
foreach ($workers as $worker) {
$packet = $worker->getPacket();
$job_pid = (int) $packet['job_pid'];
if ($job_pid and posix_kill($job_pid, 0)) {
if (posix_kill($job_pid, SIGUSR1)) {
$this->log('Worker <pop>' . $worker . '</pop> running job SIGUSR1 signal sent.');
} else {
$this->log('Worker <pop>' . $worker . '</pop> <error>running job SIGUSR1 signal could not be sent.</error>');
}
} else {
$this->log('Worker <pop>' . $worker . '</pop> has no running job.');
}
}
}
示例12: getTestCaseClosure
/**
* @param $strTestFile - the file to check for test-cases
* @param $arrDefinedFunctions - all user-definied functions until now
* @param $intParentPID - the process-pid of the parent
*
* @throw \Exception - if first parameter is not a string
* @throw \Exception - if given file is not a file nor readable
* @throw \Exception - if given parent-id is not an integer
*
* create a closure for execution in a fork. it will:
* - include the given test-file
* - find test-cases definied in test-file
* - send list of test-cases to queue
* - send SIGTERM to parent id and exit
*
**/
function getTestCaseClosure($strTestFile, array $arrDefinedFunctions, $intParentPID)
{
if (!is_string($strTestFile)) {
throw new \Exception("first given parameter is not a string");
}
if (!is_file($strTestFile) || !is_readable($strTestFile)) {
throw new \Exception("given file is not a file or not readable: {$strTestFile}");
}
if (!is_int($intParentPID)) {
throw new \Exception("third given parameter is not an integer");
}
return function () use($strTestFile, $arrDefinedFunctions, $intParentPID) {
include $strTestFile;
# get test-cases
$arrAllFunctions = get_defined_functions();
$arrUserFunctions = $arrAllFunctions['user'];
$arrDefinedFunctions = array_diff($arrUserFunctions, $arrDefinedFunctions);
$arrTestCases = array();
foreach ($arrDefinedFunctions as $strFunction) {
if (fnmatch('aphpunit\\testcases\\test*', $strFunction, FNM_NOESCAPE)) {
$arrTestCases[] = $strFunction;
}
}
# collect all information in this structure
$arrMsgContent = array('file' => $strTestFile, 'functions' => $arrTestCases);
# send the result to the queue
$objQueue = new \SimpleIPC(QUEUE_IDENTIFIER);
$objQueue->send(serialize($arrMsgContent));
# kill thread after sending parent a SIGTERM
posix_kill($intParentPID, SIGTERM);
exit;
};
}
示例13: testRunningDaemonWithResistingWorker
public function testRunningDaemonWithResistingWorker()
{
$writer = $this->getFileWriter();
$fork = $this->outerManager->fork(function () use($writer) {
$handler = new NullHandler();
$builder = new Builder(array('worker1' => function () use($writer) {
$writer("worker1.call");
}, 'worker2' => array('startup' => function () use($writer) {
pcntl_signal(SIGQUIT, SIG_IGN);
pcntl_signal(SIGINT, SIG_IGN);
$writer("worker2.startup");
}, 'loop' => function () use($writer) {
$writer("worker2.call");
}, 'interval' => 1)));
$builder->setLogger(new Logger('test', array($handler)))->setShutdownTimeout(3);
$daemon = $builder->build();
$daemon->setProcessName('testing');
$daemon->run();
});
sleep(1);
$start = time();
$fork->kill(SIGQUIT);
while (posix_kill($fork->getPid(), 0)) {
pcntl_waitpid($fork->getPid(), $status, WNOHANG | WUNTRACED);
usleep(100000);
}
$end = time();
$diff = $end - $start;
$this->assertTrue($diff >= 2 && $diff <= 4, 'Has been killed in shutdown interval');
$content = file_get_contents($this->tempFile);
$this->assertSame(1, preg_match_all('/worker1\\.call/', $content));
$this->assertSame(1, preg_match_all('/worker2\\.startup/', $content));
$calls = preg_match_all('/worker2\\.call/', $content);
$this->assertTrue($calls >= 3 && $calls <= 5, 'Expected amount of worker2 calls');
}
示例14: killAll
public function killAll()
{
/** @var \Aurora\Worker $worker */
foreach ($this->workers as $worker) {
posix_kill($worker->getPid(), SIGKILL);
}
}
示例15: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$id = $input->getArgument('id');
// Do a cleanup
$worker = new Resque\Worker('*');
$worker->cleanup();
if ($id) {
if (false === ($worker = Resque\Worker::hostWorker($id))) {
$this->log('There is no worker with id "' . $id . '".', Resque\Logger::ERROR);
return;
}
$workers = array($worker);
} else {
$workers = Resque\Worker::hostWorkers();
}
if (!count($workers)) {
$this->log('<warn>There are no workers on this host</warn>');
}
$sig = $input->getOption('force') ? 'TERM' : 'QUIT';
foreach ($workers as $worker) {
if (posix_kill($worker->getPid(), constant('SIG' . $sig))) {
$this->log('Worker <pop>' . $worker . '</pop> ' . $sig . ' signal sent.');
} else {
$this->log('Worker <pop>' . $worker . '</pop> <error>could not send ' . $sig . ' signal.</error>');
}
}
}