本文整理汇总了PHP中posix_setsid函数的典型用法代码示例。如果您正苦于以下问题:PHP posix_setsid函数的具体用法?PHP posix_setsid怎么用?PHP posix_setsid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了posix_setsid函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
开发者ID:busytoby,项目名称:gitrbug,代码行数:27,代码来源:queue_download.php
示例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);
}
开发者ID:panlatent,项目名称:aurora,代码行数:37,代码来源:Daemon.php
示例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;
}
开发者ID:wedgwood,项目名称:serverbench.php,代码行数:34,代码来源:Util.php
示例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();
}
}
开发者ID:whenjonny,项目名称:tupppai-tools,代码行数:32,代码来源:daemon.php
示例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;
}
开发者ID:nexxes,项目名称:php-daemonhelper,代码行数:41,代码来源:Daemon.php
示例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;
}
}
}
开发者ID:rakesh-mohanta,项目名称:scalr,代码行数:30,代码来源:Manager.php
示例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;
}
开发者ID:Bit-Wasp,项目名称:node-php,代码行数:33,代码来源:StartCommand.php
示例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;
}
开发者ID:seiran1944,项目名称:slate,代码行数:36,代码来源:main.php
示例9: detach
private function detach()
{
$rv = posix_setsid();
if ($rv === -1) {
Log::error("detach failed");
die;
}
}
开发者ID:robkaper,项目名称:kiki,代码行数:8,代码来源:daemon.php
示例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;
}
开发者ID:alexanderc,项目名称:threadator,代码行数:12,代码来源:TThreadControl.php
示例11: daemon
function daemon()
{
if (pcntl_fork() != 0) {
exit;
}
posix_setsid();
if (pcntl_fork() != 0) {
exit;
}
pcntl_signal(SIGTERM, "handler");
}
开发者ID:sushi-k,项目名称:epgrec,代码行数:11,代码来源:storeProgram.php
示例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();
}
开发者ID:nbtscommunity,项目名称:phpfnlib,代码行数:12,代码来源:umd.php
示例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;
}
开发者ID:phpdn,项目名称:framework,代码行数:56,代码来源:ProcessManage.php
示例14: daemonize
/**
* 进程daemon化
* @return [type] [description]
*/
public static function daemonize()
{
// fork子进程,父进程退出
if (0 != pcntl_fork()) {
exit;
}
// 设置当前进程为会话组长
posix_setsid();
// fork脱离终端
if (0 != pcntl_fork()) {
exit;
}
}
开发者ID:hitzheng,项目名称:ares,代码行数:17,代码来源:proc.php
示例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);
}
开发者ID:shakaran,项目名称:powerline-server,代码行数:13,代码来源:QueueCommand.php
示例16: daemonize
function daemonize()
{
$child = pcntl_fork();
if ($child) {
exit(0);
// kill parent
}
posix_setsid();
// become session leader
umask(0);
// clear umask
return posix_getpid();
}
开发者ID:jeremy-cayrasso,项目名称:dtc,代码行数:13,代码来源:dtc-stats-daemon.php
示例17: work
/**
* Daemonize a Closure object.
*
* @throws \Exception
*
* @param array $options Set of options
* @param callable $callable Closure object to daemonize
*
* @return bool True on success, throws an Exception otherwise
*/
public static function work(array $options, callable $callable)
{
if (!extension_loaded('pcntl')) {
throw new \Exception('pcntl extension required');
}
if (!extension_loaded('posix')) {
throw new \Exception('posix extension required');
}
if (!isset($options['pid'])) {
throw new \Exception('pid not specified');
}
$options = $options + array('stdin' => '/dev/null', 'stdout' => '/dev/null', 'stderr' => 'php://stdout');
if (($lock = @fopen($options['pid'], 'c+')) === false) {
throw new \Exception('unable to open pid file ' . $options['pid']);
}
if (!flock($lock, LOCK_EX | LOCK_NB)) {
throw new \Exception('could not acquire lock for ' . $options['pid']);
}
switch ($pid = pcntl_fork()) {
case -1:
throw new \Exception('unable to fork');
case 0:
break;
default:
fseek($lock, 0);
ftruncate($lock, 0);
fwrite($lock, $pid);
fflush($lock);
return;
}
if (posix_setsid() === -1) {
throw new \Exception('failed to setsid');
}
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
if (!($stdin = fopen($options['stdin'], 'r'))) {
throw new \Exception('failed to open STDIN ' . $options['stdin']);
}
if (!($stdout = fopen($options['stdout'], 'w'))) {
throw new \Exception('failed to open STDOUT ' . $options['stdout']);
}
if (!($stderr = fopen($options['stderr'], 'w'))) {
throw new \Exception('failed to open STDERR ' . $options['stderr']);
}
pcntl_signal(SIGTSTP, SIG_IGN);
pcntl_signal(SIGTTOU, SIG_IGN);
pcntl_signal(SIGTTIN, SIG_IGN);
pcntl_signal(SIGHUP, SIG_IGN);
call_user_func($callable, $stdin, $stdout, $stderr);
}
开发者ID:wikimedia,项目名称:wikimedia-fundraising-crm-vendor,代码行数:61,代码来源:Daemon.php
示例18: execute
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (defined('HHVM_VERSION')) {
$output->writeln('<error>This command is not supported on HHVM.</error>');
return 1;
}
if (!extension_loaded('pcntl')) {
$output->writeln('<error>This command needs the pcntl extension to run.</error>');
$output->writeln('You can either install it or use the <info>server:run</info> command instead to run the built-in web server.');
return 1;
}
$env = $this->getContainer()->getParameter('kernel.environment');
if ('prod' === $env) {
$output->writeln('<error>Running PHP built-in server in production environment is NOT recommended!</error>');
}
$pid = pcntl_fork();
if ($pid < 0) {
$output->writeln('<error>Unable to start the server process</error>');
return 1;
}
$address = $input->getArgument('address');
if ($pid > 0) {
$output->writeln(sprintf('<info>Web server listening on http://%s</info>', $address));
return;
}
if (posix_setsid() < 0) {
$output->writeln('<error>Unable to set the child process as session leader</error>');
return 1;
}
if (null === ($process = $this->createServerProcess($output, $address, $input->getOption('docroot'), $input->getOption('router'), $env, null))) {
return 1;
}
$process->disableOutput();
$process->start();
$lockFile = $this->getLockFile($address);
touch($lockFile);
if (!$process->isRunning()) {
$output->writeln('<error>Unable to start the server process</error>');
unlink($lockFile);
return 1;
}
// stop the web server when the lock file is removed
while ($process->isRunning()) {
if (!file_exists($lockFile)) {
$process->stop();
}
sleep(1);
}
}
开发者ID:jmikola,项目名称:symfony,代码行数:52,代码来源:ServerStartCommand.php
示例19: deamonlize
function deamonlize()
{
$pid = pcntl_fork();
if ($pid < 0) {
cy_log(CYE_ERROR, "pcntl_fork({$key}) error.");
exit(-1);
}
if ($pid > 0) {
exit(0);
}
$d = posix_setsid();
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
}
开发者ID:xiaoyjy,项目名称:retry,代码行数:15,代码来源:main.php
示例20: daemon
/**
* 摆脱终端,使进程成为新的会话组长
*/
function daemon()
{
$pid = pcntl_fork();
if ($pid == -1) {
die("fork failed for daemon");
} else {
if ($pid) {
exit;
} else {
//摆脱终端,使进程成为新的会话组长
if (posix_setsid() == -1) {
die("could not detach from terminal");
}
}
}
}
开发者ID:xjlelf,项目名称:php_test,代码行数:19,代码来源:index.php
注:本文中的posix_setsid函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论