diff options
| author | Paul Buetow <paul@buetow.org> | 2011-02-06 11:02:18 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2011-02-06 11:02:18 +0000 |
| commit | 5e843d61702c967748dfa9c84860152113831542 (patch) | |
| tree | 7bd6c50c07f8efc3c465dd806568496d24be5980 | |
| parent | ef8d41048c51078ad27ee2f29d0e667073aeee98 (diff) | |
added control.sh which includes
start, stop and logrotate
removed obsolete start.sh and stop.sh scripts
logrotating now works in perldaemon.pl
| -rw-r--r-- | control.sh | 20 | ||||
| -rw-r--r-- | perldaemon.conf | 1 | ||||
| -rw-r--r-- | perldaemon.pl | 36 | ||||
| -rwxr-xr-x | start.sh | 3 | ||||
| -rwxr-xr-x | stop.sh | 3 |
5 files changed, 53 insertions, 10 deletions
diff --git a/control.sh b/control.sh new file mode 100644 index 0000000..346f736 --- /dev/null +++ b/control.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +case $1 in + start) + perl perldaemon.pl perldaemon.conf + ;; + + stop) + kill $(cat perldaemon.pid) + ;; + + logrotate) + kill -HUP $(cat perldaemon.pid) + ;; + + *) + echo "Usage: $0 <start|stop|logrotate>" + exit 1 + ;; +esac diff --git a/perldaemon.conf b/perldaemon.conf index f54f546..cf036f6 100644 --- a/perldaemon.conf +++ b/perldaemon.conf @@ -2,4 +2,3 @@ daemon.wd = ./ daemon.pidfile = ./perldaemon.pid daemon.logfile = ./perldaemon.log -daemon.truncatelog = yes diff --git a/perldaemon.pl b/perldaemon.pl index c5580da..3d2d11a 100644 --- a/perldaemon.pl +++ b/perldaemon.pl @@ -5,6 +5,7 @@ use strict; use warnings; use POSIX qw(setsid); +use Shell qw(mv); use constant VERSION => 1; @@ -44,6 +45,19 @@ sub err ($$) { die "$msg\n"; } +sub rotatelog ($) { + my $config = shift; + my $logfile = $config->{'daemon.logfile'}; + + logmsg $config => 'Rotating logfile'; + + my @t = localtime(); + $t[5] += 1900; + my $timestr = "$t[5]$t[4]$t[3]-$t[2]$t[1]$t[0]"; + + mv($logfile, "$logfile.$timestr"); +} + sub checkpid ($) { my $config = shift; @@ -93,7 +107,7 @@ sub readconfig ($) { # Check my $msg = 'Missing property:'; - foreach (qw(wd pidfile logfile truncatelog)) { + foreach (qw(wd pidfile logfile)) { my $key = "daemon.$_"; die "$msg $key\n" unless exists $config{$key}; } @@ -124,11 +138,26 @@ sub daemonize ($) { logmsg $config => 'Daemonizing completed'; } +sub sighandlers ($) { + my $config = shift; + + $SIG{TERM} = sub { + # On shutdown + logmsg $config => 'Received SIGTERM. Shutting down....'; + unlink $config->{'daemon.pidfile'}; + exit 0; + }; + + $SIG{HUP} = sub { + # On logrotate + logmsg $config => 'Received SIGHUP.'; + rotatelog $config; + }; +} + sub prestartup ($) { my $config = shift; checkpid $config; - - trunc $config->{'daemon.logfile'} if $config->{'daemon.truncatelog'} eq 'yes'; } sub daemonloop ($) { @@ -145,6 +174,7 @@ my $config = readconfig shift; prestartup $config; daemonize $config; +sighandlers $config; daemonloop $config; diff --git a/start.sh b/start.sh deleted file mode 100755 index 30b7e31..0000000 --- a/start.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -perl perldaemon.pl perldaemon.conf diff --git a/stop.sh b/stop.sh deleted file mode 100755 index 7901290..0000000 --- a/stop.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -kill $(cat perldaemon.pid) |
