* daemon.php skeleton for daemon that terminates child processes

* inverter.php uses new daemon and schedules itself to start and stop
This commit is contained in:
2012-12-17 10:37:55 +01:00
parent 25071dd2cb
commit e96acedd28
2 changed files with 90 additions and 61 deletions

68
opt/inverter/daemon.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
require_once 'System/Daemon.php'; // pear install -f System_Daemon
define('FILE_DAEMON_START', 'daemon_start.sh');
define('FILE_DAEMON_STOP', 'daemon_stop.sh');
define('MODE', 0755);
define('PROCESS_POLL', 30);
/* Install daemon */
if (isset($argv[1]) && $argv[1] == 'install') {
daemon_install();
}
function daemon_init() {
global $sName;
/* Daemon options */
System_Daemon::setOptions(array(
'appName' => NAME,
'appDescription' => '',
'authorName' => '',
'authorEmail' => ''));
/* Derive process name */
$sName = basename(substr(TASK, 0, strpos(TASK, ' ')));
}
function daemon_install() {
System_Daemon::writeAutoRun(); // update-rc.d %s defaults
/* Write scripts for scheduling with at */
if (isset($argv[2]) && $argv[2] == 'schedule') {
if (!file_exists(FILE_DAEMON_START)) {
file_put_contents(FILE_DAEMON_START, sprintf("#!/bin/bash\nservice %s start", NAME));
chmod(FILE_DAEMON_START, MODE);
}
if (!file_exists(FILE_DAEMON_STOP)) {
file_put_contents(FILE_DAEMON_STOP, sprintf("#!/bin/bash\nservice %s stop", NAME));
chmod(FILE_DAEMON_STOP, MODE);
}
}
}
function daemon_run() {
global $rProcess;
/* Hook onto daemon termination handler */
System_Daemon::setSigHandler(SIGTERM, 'daemon_sigterm_handler');
/* Start deamon */
System_Daemon::start();
while (!System_Daemon::isDying()) {
System_Daemon::info('Open process');
$rProcess = proc_open(TASK, array(), $aPipes);
do {
System_Daemon::isRunning(); // required for deamon to respond properly
sleep(PROCESS_POLL); // gets interrupted on process termination
$aStatus = proc_get_status($rProcess);
} while ($aStatus['running']);
System_Daemon::info('Process ended');
}
}
function daemon_sigterm_handler($iSigNo) {
global $sName;
system(sprintf('pkill %s', $sName));
System_Daemon::stop();
}

View File

@@ -1,17 +1,17 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
require_once 'functions.php'; require_once 'functions.php';
require_once 'System/Daemon.php'; // pear install -f System_Daemon require_once 'daemon.php';
define('NAME', 'inverter'); define('NAME', 'inverter');
define('TASK', '/opt/inverter/inverter.pl > /dev/null'); define('TASK', '/opt/inverter/inverter.pl > /dev/null');
define('CWD', '/opt/inverter/'); define('CWD', '/opt/inverter/');
define('MODE', 0755);
define('FILE_DAEMON_START', 'daemon_start.sh');
define('FILE_DAEMON_STOP', 'daemon_stop.sh');
define('DEFAULT_WAKE', '7:00'); define('DEFAULT_WAKE', '7:00');
define('DEFAULT_SLEEP', '19:00'); define('DEFAULT_SLEEP', '19:00');
/* Initialize */
chdir(CWD); chdir(CWD);
daemon_init();
/* Remove previous at entries */ /* Remove previous at entries */
foreach (explode("\n", trim(command('atq 2> /dev/null'))) as $sJob) { foreach (explode("\n", trim(command('atq 2> /dev/null'))) as $sJob) {
@@ -23,75 +23,36 @@ foreach (explode("\n", trim(command('atq 2> /dev/null'))) as $sJob) {
} }
} }
/* Inverter daemon */
System_Daemon::setOptions(array(
'appName' => NAME,
'appDescription' => '',
'authorName' => '',
'authorEmail' => ''));
/* Install service */
if (isset($argv[1]) && $argv[1] == 'install') {
System_Daemon::writeAutoRun(); // update-rc.d %s defaults
/* Write scripts for scheduling with at */
if (!file_exists(FILE_DAEMON_START)) {
file_put_contents(FILE_DAEMON_START, sprintf("#!/bin/bash\nservice %s start", NAME));
chmod(FILE_DAEMON_START, MODE);
}
if (!file_exists(FILE_DAEMON_STOP)) {
file_put_contents(FILE_DAEMON_STOP, sprintf("#!/bin/bash\nservice %s stop", NAME));
chmod(FILE_DAEMON_STOP, MODE);
}
exit;
}
/* Wake at sunrise, sleep at sunset */ /* Wake at sunrise, sleep at sunset */
$aTwilight = getTwilight(date('Y'), date('z')); $aTwilight = getTwilight(date('Y'), date('z'));
$fWake = getHour($sWake = isset($aTwilight) ? $aTwilight[1] : DEFAULT_WAKE); $fWake = getHour($sWake = isset($aTwilight) ? $aTwilight[1] : DEFAULT_WAKE);
$fSleep = getHour($sSleep = isset($aTwilight) ? $aTwilight[3] : DEFAULT_SLEEP); $fSleep = getHour($sSleep = isset($aTwilight) ? $aTwilight[3] : DEFAULT_SLEEP);
System_Daemon::info(sprintf('Be awake between %s and %s', $sWake, $sSleep)); System_Daemon::info(sprintf('Be awake between %s and %s', $sWake, $sSleep));
/* Start deamon */ /* Check appropriate state */
System_Daemon::start(); $fNow = getHour();
$bStop = $bAlarm = false; if (!($bAwake = $fNow >= $fWake)) {
System_Daemon::info('Too early to wake!');
while (!$bStop && !System_Daemon::isDying()) { } else if ($bSleep = $fNow >= $fSleep) {
/* Check for current need to be awake */ System_Daemon::info('Time to sleep!');
$fNow = getHour();
if (!($bAwake = $fNow >= $fWake)) {
System_Daemon::info('Too early to wake!');
} else if ($bSleep = $fNow >= $fSleep) {
System_Daemon::info('Time to sleep!');
}
if ($bAwake && !$bSleep) {
/* Schedule next sleep time */
/*if (!$bAlarm) {
$sTime = date('H:i', strtotime($sSleep));
System_Daemon::info(sprintf('Schedule sleep at %s', $sTime));
command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_STOP, $sTime));
$bAlarm = true;
}*/
/* Execute task */
System_Daemon::info('Running task');
command(TASK);
System_Daemon::info('Task ended');
} else {
$bStop = true;
}
} }
/* Schedule next wake time */
schedule_wake(); schedule_wake();
/* Stop daemon */ if ($bAwake && !$bSleep) {
System_Daemon::stop(); schedule_sleep();
daemon_run();
}
function schedule_wake() { function schedule_wake() {
global $sWake; global $sWake;
$sTime = date('H:i', strtotime($sWake)); // ignore slight deviation for next day $sTime = date('H:i', strtotime($sWake)); // ignore slight deviation for next day
System_Daemon::info(sprintf('Waiting untill %s', $sTime)); System_Daemon::info(sprintf('Schedule wake at %s', $sTime));
command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_START, $sTime)); command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_START, $sTime));
} }
function schedule_sleep() {
global $sSleep;
$sTime = date('H:i', strtotime($sSleep));
System_Daemon::info(sprintf('Schedule sleep at %s', $sTime));
command(sprintf('at -f %s %s 2> /dev/null', FILE_DAEMON_STOP, $sTime));
}