本文整理汇总了PHP中posix_setsid函数的典型用法代码示例。如果您正苦于以下问题:PHP posix_setsid函数的具体用法?PHP posix_setsid怎么用?PHP posix_setsid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了posix_setsid函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: spawnDownloader
function spawnDownloader($data)
{
function thread_shutdown()
{
posix_kill(posix_getpid(), SIGHUP);
}
if ($pid = pcntl_fork()) {
return;
}
// spawn to child process
fclose(STDIN);
// close descriptors
fclose(STDOUT);
fclose(STDERR);
register_shutdown_function('thread_shutdown');
// zombie-proof
if (posix_setsid() < 0) {
return;
}
// re-parent child to kernel
if ($pid = pcntl_fork()) {
return;
}
// now in daemonized downloader
// download stuff
return;
}
示例2: start
/**
* @return \Generator|void
* @throws \Aurora\Console\Exception
*/
public function start()
{
if (0 < ($pid = pcntl_fork())) {
return;
} elseif (-1 == $pid) {
throw new Exception("Unable to create daemon process");
}
posix_setsid();
if (0 < ($pid = pcntl_fork())) {
// 禁止进程重新打开控制终端
exit(0);
} elseif (-1 == $pid) {
throw new Exception("Unable to create the process of leaving the terminal");
}
chdir('/');
umask(0);
$user = $this->config->get('master_user', 'nobody');
$group = $this->config->get('master_user_group', '');
Posix::setUser($user, $group);
$filename = $this->config->get('pid_file_save_path', '/var/run/aurora.pid');
if (!($fd = @fopen($filename, 'w+'))) {
throw new Exception("PID file creation failed" . error_get_last()['message']);
} elseif (!flock($fd, LOCK_EX | LOCK_NB)) {
throw new Exception("Unable to lock the PID file");
}
fwrite($fd, posix_getpid());
// $this->closeStdDescriptors();
call_user_func(yield, $this->config);
flock($fd, LOCK_UN);
fclose($fd);
unlink($filename);
exit(0);
}
示例3: daemon
public static function daemon($nochdir = true, $noclose = true)
{
umask(0);
$pid = pcntl_fork();
if ($pid > 0) {
exit;
} elseif ($pid < 0) {
return false;
} else {
// nothing to do ...
}
$pid = pcntl_fork();
if ($pid > 0) {
exit;
} elseif ($pid < 0) {
return false;
} else {
// nothing to do ...
}
$sid = posix_setsid();
if ($sid < 0) {
return false;
}
if (!$nochdir) {
chdir('/');
}
umask(0);
if (!$noclose) {
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
}
return true;
}
示例4: daemonize
public function daemonize()
{
global $stdin, $stdout, $stderr;
global $argv;
set_time_limit(0);
if (php_sapi_name() != "cli") {
die("only run in command line mode\n");
}
if ($this->is_sington == true) {
$this->pid_file = $this->info_dir . "/" . __CLASS__ . "_" . substr(basename($argv[0]), 0, -4) . ".pid";
$this->checkPidfile();
}
umask(0);
if (pcntl_fork() != 0) {
exit;
}
posix_setsid();
if (pcntl_fork() != 0) {
exit;
}
chdir("/");
$this->setUser($this->user) or die("cannot change owner");
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
$stdin = fopen($this->output, 'r');
$stdout = fopen($this->output, 'a');
$stderr = fopen($this->output, 'a');
if ($this->is_sington == true) {
$this->createPidfile();
}
}
示例5: daemonize
/**
* Daemonize the current process so it can run in the background.
*
* If $pidfile is supplied, the process ID is written there.
* Provide absolute path names for the parameters to avoid file not found errors or logs inside the source code folder.
*
* If an error occurred, a RuntimeException is thrown.
*
* @param string $pidfile File to write the process ID of the daemon to
* @param string $stderr File to redirect STDERR to
* @param string $stdout File to redirect STDOUT to
* @param string $stdin File to read STDIN from
* @throws \RuntimeException
* @return true
*/
public static function daemonize($pidfile = null, $stderr = '/dev/null', $stdout = '/dev/null', $stdin = '/dev/null')
{
// Allow only cli scripts to daemonize, otherwise you may confuse your webserver
if (\php_sapi_name() !== 'cli') {
throw new \RuntimeException('Can only daemonize a CLI process!');
}
self::checkPID($pidfile);
self::reopenFDs($stdin, $stdout, $stderr);
if (($pid1 = @\pcntl_fork()) < 0) {
throw new \RuntimeException('Failed to fork, reason: "' . \pcntl_strerror(\pcntl_get_last_error()) . '"');
} elseif ($pid1 > 0) {
exit;
}
if (@posix_setsid() === -1) {
throw new \RuntimeException('Failed to become session leader, reason: "' . \posix_strerror(\posix_get_last_error()) . '"');
}
if (($pid2 = @\pcntl_fork()) < 0) {
throw new \RuntimeException('Failed to fork, reason: "' . \pcntl_strerror(\pcntl_get_last_error()) . '"');
} elseif ($pid2 > 0) {
exit;
}
chdir('/');
umask(022);
self::writePID($pidfile);
return true;
}
示例6: forkChild
protected function forkChild($type)
{
$this->logger->error("Fork child process");
$pid = pcntl_fork();
if ($pid == -1) {
// Cannot fork child
throw new Scalr_System_Ipc_Exception("Cannot fork child process");
} else {
if ($pid) {
// Current process
$this->shm->put($type, $pid);
$this->logger->error(sprintf("Child PID: %s was forked", $pid));
} else {
if (posix_getpid() == $this->shm->get($type)) {
$this->logger->error("Detaching process from terminatal");
if (posix_setsid() == -1) {
throw new Scalr_System_Ipc_Exception("Cannot detach process from terminal");
}
}
if ($type == self::GEARMAN_CLIENT) {
$client = new Scalr_Service_Gearman_Client($this->servers);
$client->mainLoop();
} elseif ($type == self::GEARMAN_WORKER) {
$worker = new Scalr_Service_Gearman_Worker($this->servers);
$worker->mainLoop();
}
exit;
}
}
}
示例7: execute
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = (new ConfigLoader())->load();
// Create the child process
// All the code after pcntl_fork () will be performed by two processes: parent and child
if ($config->getItem('config', 'daemon', false)) {
$child_pid = pcntl_fork();
if ($child_pid) {
// Exit from the parent process that is bound to the console
exit;
}
// Make the child as the main process.
posix_setsid();
}
$math = Bitcoin::getMath();
$params = new Params($math);
$loop = \React\EventLoop\Factory::create();
$db = new DebugDb(Db::create($config));
$node = new BitcoinNode($config, $params, $db);
$container = new Container();
$container['debug'] = function (Container $c) use($node) {
$context = $c['zmq'];
return new ZmqDebug($node, $context);
};
$this->setupServices($container, $node, $loop, $config, $db);
$loop->run();
return 0;
}
示例8: execute
/**
* Create a daemon process.
*
* @return DaemonHttpApplication Return self to support chaining.
*/
public function execute()
{
// Create first child.
if (pcntl_fork()) {
// I'm the parent
// Protect against Zombie children
pcntl_wait($status);
exit;
}
// Make first child as session leader.
posix_setsid();
// Create second child.
$pid = pcntl_fork();
if ($pid) {
// If pid not 0, means this process is parent, close it.
$this->processId = $pid;
$this->storeProcessId();
exit;
}
$this->addLog('Daemonized');
fwrite(STDOUT, "Daemon Start\n-----------------------------------------\n");
$this->registerSignalHandler();
// Declare ticks to start signal monitoring. When you declare ticks, PCNTL will monitor
// incoming signals after each tick and call the relevant signal handler automatically.
declare (ticks=1);
while (true) {
$this->doExecute();
sleep(1);
}
return $this;
}
示例9: detach
private function detach()
{
$rv = posix_setsid();
if ($rv === -1) {
Log::error("detach failed");
die;
}
}
示例10: detach
/**
* @throws \RuntimeException
*/
public final function detach()
{
if (-1 === posix_setsid()) {
throw new \RuntimeException("Unable to detach thread");
}
$this->pid = posix_getpid();
$this->parentPid = null;
$this->detached = true;
}
示例11: daemon
function daemon()
{
if (pcntl_fork() != 0) {
exit;
}
posix_setsid();
if (pcntl_fork() != 0) {
exit;
}
pcntl_signal(SIGTERM, "handler");
}
示例12: UMDDaemon
function UMDDaemon($sockaddr)
{
$this->sockaddr = $sockaddr;
$this->openlog('/tmp/umd' . basename($sockaddr) . '.log');
unlink($this->sockaddr);
$this->file = __FILE__;
$listen = socket_create(AF_UNIX, SOCK_STREAM, 0) or die('Cannot create socket');
socket_bind($listen, $this->sockaddr) or die('Cannot bind');
socket_listen($listen) or die('Cannot listen');
$this->register_listener($listen);
posix_setsid();
}
示例13: demonize
/**
* 初始化守护进程
*
*/
private static function demonize()
{
php_sapi_name() != 'cli' && die('should run in cli');
umask(0);
$pid = pcntl_fork();
if ($pid < 0) {
die("can't Fork!");
} else {
if ($pid > 0) {
exit;
}
}
if (posix_setsid() === -1) {
//使进程成为会话组长。让进程摆脱原会话的控制;让进程摆脱原进程组的控制;
die('could not detach');
}
$pid = pcntl_fork();
if ($pid === -1) {
die("can't fork2!");
} elseif ($pid > 0) {
self::message('start success!');
exit;
}
defined('STDIN') && fclose(STDIN);
defined('STDOUT') && fclose(STDOUT);
defined('STDERR') && fclose(STDERR);
$stdin = fopen(self::$log, 'r');
$stdout = fopen(self::$log, 'a');
$stderr = fopen(self::$log, 'a');
self::setUser(self::$user);
file_put_contents(self::$pidFile, posix_getpid()) || die("can't create pid file");
self::setProcessName('master');
pcntl_signal(SIGINT, array('\\' . __CLASS__, 'signalHandler'), false);
pcntl_signal(SIGUSR1, array('\\' . __CLASS__, 'signalHandler'), false);
file_put_contents(self::$status, '<?php return ' . var_export(array(), true) . ';', LOCK_EX);
self::createChildrenProcess();
while (true) {
pcntl_signal_dispatch();
$pid = pcntl_wait($status, WUNTRACED);
pcntl_signal_dispatch();
if ($pid > 0) {
$status = self::getStatus();
if (isset($status['pid'][$pid])) {
unset($status['pid'][$pid]);
file_put_contents(self::$status, '<?php return ' . var_export($status, true) . ';', LOCK_EX);
}
self::createChildrenProcess();
}
sleep(1);
}
return;
}
示例14: daemonize
/**
* 进程daemon化
* @return [type] [description]
*/
public static function daemonize()
{
// fork子进程,父进程退出
if (0 != pcntl_fork()) {
exit;
}
// 设置当前进程为会话组长
posix_setsid();
// fork脱离终端
if (0 != pcntl_fork()) {
exit;
}
}
示例15: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$childPid = pcntl_fork();
if ($childPid) {
$output->writeln('Creating of child fork has been succesfull finished with pid = ' . $childPid);
exit;
}
posix_setsid();
$rabbitMQCommand = $this->getApplication()->find('rabbitmq:consumer');
$arguments = array('command' => 'rabbitmq:consumer', 'name' => 'push');
$input = new ArrayInput($arguments);
$rabbitMQCommand->run($input, $output);
}