All pastes #800954 Raw Edit

Daemonized

public php v1 · immutable
#800954 ·published 2007-11-28 21:32 UTC
rendered paste body
#!/usr/local/bin/php5 -f <?php	/**	 * Daemonize the process	 */	if ( pcntl_fork() ){		exit(0);	}		posix_setsid();			if ( pcntl_fork() ){		exit(0);	}	/**	 * Set the user permissions on the script 	 */	$pw = posix_getpwnam('nobody');	posix_setuid($pw['uid']);	posix_setgid($pw['gid']);			/**	 * Get an exclusive lock or exit!	 */	if ( false === startLock(__FILE__) ){		writeError('Could not get exclusive lock');		exit(1);	}	/**	 * Must declare ticks in main execution!!	 */	declare(ticks = 1);		/**	 * Presence of stop file prevents this daemon from starting/running	 */	define('SCRIPT_STOP_FILE' , '/tmp/' . basename(__FILE__) . '.stop');		/**	 * Require sig handler class and instantiate	 */	$sigHandler = new signalHandler(__FILE__);		/**	 * Make sure we're OK to run 	 */	if ( !$sigHandler->checkRun() ){		exit($sigHandler->getExitCode());	}	do{		if ( !getDB('slave')->Ping() || !getDB('master')->Ping() ){			writeError('[ERROR] Could not ping DB server(s)!');			exit(1);		}				foreach( array_keys($enquire) as $sku ){			if ( pcntl_fork() > 0 ){				continue;			} else {					posix_setsid();									if ( pcntl_fork() ){					exit(0);				}					processInventoryEnquire($sku, $enquire);				exit(0);			}				}		sleep(MAX_CHILDREN_LIMIT/2);	} while ($sigHandler->checkRun());	exit($sigHandler->getExitCode());?>