summaryrefslogtreecommitdiff
path: root/daemonize/daemonize_lib.php
blob: d6a16da7a7894bad6a7c77e9f3bff225e9ce3c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

function daemonize_init( $pPidfile ) {
	global $gDaemonTouchTimer;
	// Log our PID as the first thing, so other processes don't try to kill us.
	echo "DAEMON ".getmypid()." : writing pidfile $pPidfile\n";
	if( !$fp = fopen($pPidfile, "w") ) {
		die( "could not open pid file: $pPidfile\n\n" );
	}
	fwrite($fp, posix_getpid()."\n".time()."\n");
	fclose($fp);
	$gDaemonTouchTimer = 0; // Timer so we only touch the pidfile sometimes. 
}

function daemonize_refresh( $pPidfile ) {
	global $gDaemonTouchTimer;
	static $ticks = 0;
	if ($gDaemonTouchTimer + 15 < time()) { // So the revivifier can tell we've hung.
		if( ($ticks % 10) == 0 ) {
			// keep the noise down and only log a message once per minute
			echo date( 'd/M/Y:H:i:s O' )." - DAEMON ".getmypid()." : Touching the file $pPidfile to ".time()."\n";
		}
		if( $fp = fopen($pPidfile, "w") ) {
			fwrite($fp, posix_getpid()."\n".time()."\n");
			fclose($fp);
			$gDaemonTouchTimer = time();
		} else {
			die( "could not touch pid file: $pPidfile\n\n" );
		}
		$ticks++;
	} 
}