if(count($_SERVER["argv"]) < 3) die("Usage: {$_SERVER["argv"][0]} bind_ip uri [user] [debug] [foreground]\n"); $debug = false; if(isset($_SERVER["argv"][4])) $debug = (bool)$_SERVER["argv"][4]; if(isset($_SERVER["argv"][5])) { $foreground = (bool)$_SERVER["argv"][5]; } else { $foreground = $debug; } if (!$foreground) { if (function_exists('posix_setsid') && function_exists('pcntl_fork')) { $pid = daemonize("/tmp/httptftpserver.pid", "/"); if($pid === false) die("Failed to daemonize\n"); if($pid != 0) exit(0); } else { echo "POSIX Functions don't exist. So we can't run this in the background.\n" . "You Might want to think about running 'yum install php-process' or something equivalent\n" . "For now we will just run in the foreground\n"; } } $user = null; if(isset($_SERVER["argv"][3]) && function_exists('posix_setsid')) $user = posix_getpwnam($_SERVER["argv"][3]);
} chmod("{$workdirpath}/testcase", 0700); // Auto-register judgehost via REST // If there are any unfinished judgings in the queue in my name, // they will not be finished. Give them back. $unfinished = request('judgehosts', 'POST', 'hostname=' . urlencode($myhost)); $unfinished = dj_json_decode($unfinished); foreach ($unfinished as $jud) { $workdir = "{$workdirpath}/c{$jud['cid']}-s{$jud['submitid']}-j{$jud['judgingid']}"; @chmod($workdir, 0700); logmsg(LOG_WARNING, "Found unfinished judging j" . $jud['judgingid'] . " in my name; given back"); } } // If all startup done, daemonize if (isset($options['daemon'])) { daemonize(PIDFILE); } // Constantly check API for unjudged submissions $endpointIDs = array_keys($endpoints); $currentEndpoint = 0; while (TRUE) { // If all endpoints are waiting, sleep for a bit $dosleep = TRUE; foreach ($endpoints as $id => $endpoint) { if ($endpoint["waiting"] == FALSE) { $dosleep = FALSE; break; } } // Sleep only if everything is "waiting" and only if we're looking at the first endpoint again if ($dosleep && $currentEndpoint == 0) {
{ $child = pcntl_fork(); if ($child) { exit(0); // kill parent } posix_setsid(); // become session leader umask(0); // clear umask return posix_getpid(); } // daemonize the process so it's sits in the background // we don't do it in Debian since there, we use start-stop-daemon that does all the job if (!file_exists("/etc/debian_version")) { $pid = daemonize(); $fp = fopen("/var/run/dtc-stats-daemon.pid", "w"); fwrite($fp, $pid . "\n"); fclose($fp); } $log_fp = fopen("/var/log/dtc-stats-daemon.log", "a+"); error_reporting(E_ALL); $conf_time_delay_in_seconds = 62; fwrite($log_fp, date("Y-m-d H:i:s") . " dtc-stats-daemon starting up...\n"); $last_loop = 0; // loop until we want to shutdown... $shutdown = false; while (!$shutdown) { if ($last_loop > 0) { $time_elapsed_since_last_run = time() - $last_loop; // if the time elapsed is less than 10 minutes, sleep until it is
<?php function daemonize() { $pid = pcntl_fork(); if ($pid == -1) { die("fork(1) failed!\n"); } elseif ($pid > 0) { //让由用户启动的进程退出 exit(0); } //建立一个有别于终端的新session以脱离终端 posix_setsid(); //此时子进程已经成为无控制终端的会话组长进程,但有可能再次打开一个控制终端;再次fork,变成非会话组长进程即可。 $pid = pcntl_fork(); if ($pid == -1) { die("fork(2) failed!\n"); } elseif ($pid > 0) { //父进程退出, 剩下子进程成为最终的独立进程 exit(0); } } daemonize(); sleep(1000);
{ $result = $this->query("SELECT NOW()"); if($result === false) return false; $row = mysql_fetch_array($result); if($row === false) return false; return "ok {$row[0]}\n"; } } // daemonize, write pidfile and change working directory // SIGTERM will terminate $pid = daemonize("/tmp/mysqltftpserver.pid", "/"); if($pid === false) die("Failed to daemonize\n"); if($pid != 0) exit(0); $config = array("hostname" => "localhost", "username" => "", "password" => "", "database" => ""); $server = new MySQLTFTPServer("udp://0.0.0.0:69", $config); if(!$server->loop(&$error, posix_getpwnam("nobody"))); die("$error\n"); ?>