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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#!/usr/bin/perl
# Minimal PerlDaemon (c) 2011 Paul Buetow
use strict;
use warnings;
use Shell qw(mv);
use POSIX qw(setsid strftime);
use PerlDaemon::Logger;
use PerlDaemon::RunModules;
$| = 1;
sub trimstr (@) {
my @str =
@_;
for (@str) {
chomp;
s/^[\t\s]+//;
s/[\t\s]+$//;
}
return @str;
}
sub trunc ($) {
my $file = shift;
open my $fh, ">$file" or die "Can't write $file: $!\n";
print $fh '';
close $fh;
}
sub checkpid ($) {
my $conf = shift;
my $pidfile = $conf->{'daemon.pidfile'};
my $logger = $conf->{logger};
trunc $pidfile unless -f $pidfile;
open my $fh, $pidfile or $logger->err("Can't read pidfile $pidfile: $!");
my ($pid) = <$fh>;
close $fh;
if (defined $pid) {
chomp $pid;
$logger->err("Process with pid $pid already running") if 0 < int $pid && kill 0, $pid;
}
}
sub writepid ($) {
my $conf = shift;
my $logger = $conf->{logger};
my $pidfile = $conf->{'daemon.pidfile'};
open my $fh, ">$pidfile" or $logger->err("Can't write pidfile: $!");
print $fh "$$\n";
close $fh;
}
sub readconf ($%) {
my ($confile, %opts) = @_;
open my $fh, $confile or
die "Can't read config file $confile (specify using config=filepath)\n";
my %conf;
while (<$fh>) {
next if /^[\t\w]+#/;
s/#.*//;
my ($key, $val) = trimstr split '=', $_, 2;
next unless defined $val;
$conf{$key} = $val;
}
close $fh;
# Check
my $msg = 'Missing property:';
foreach (qw(wd loopinterval alivefile pidfile logfile daemonize)) {
my $key = "daemon.$_";
die "$msg $key\n" unless exists $conf{$key};
}
@conf{keys %opts} = values %opts;
return \%conf;
}
sub daemonize ($) {
my $conf = shift;
my $logger = $conf->{logger};
$logger->logmsg('Daemonizing...');
chdir $conf->{'daemon.wd'} or $logger->err("Can't chdir to wd: $!");
my $msg = 'Can\'t read /dev/null:';
open STDIN, '>/dev/null' or $logger->err("$msg $!");
open STDOUT, '>/dev/null' or $logger->err("$msg $!");
open STDERR, '>/dev/null' or $logger->err("$msg $!");
defined (my $pid = fork) or $logger->err("Can't fork: $!");
exit if $pid;
setsid or $logger->err("Can't start a new session: $!");
writepid $conf;
$logger->logmsg('Daemonizing completed');
}
sub sighandlers ($) {
my $conf = shift;
my $logger = $conf->{logger};
$SIG{TERM} = sub {
# On shutdown
$logger->logmsg('Received SIGTERM. Shutting down....');
unlink $conf->{'daemon.pidfile'} if -f $conf->{'daemon.pidfile'};
exit 0;
};
$SIG{HUP} = sub {
# On logrotate
$logger->logmsg('Received SIGHUP.');
$logger->rotatelog();
};
}
sub prestartup ($) {
my $conf = shift;
checkpid $conf;
}
sub alive ($) {
my $conf = shift;
}
sub daemonloop ($) {
my $conf = shift;
my $rmodule = PerlDaemon::RunModules->new($conf);
my $loopinterval = $conf->{'daemon.loopinterval'};
my $loop = shift;
for (my $i = 1;;++$i) {
$rmodule->do();
sleep $loopinterval;
alive $conf;
}
}
sub getopts (@) {
my %opts;
for my $opt (@_) {
next unless $opt =~ /=/;
my ($key, $val) = split '=', $opt, 2;
$opts{$key} = $val;
}
return %opts;
}
my %opts = getopts @ARGV;
my $conf = readconf $opts{config}, %opts;
$conf->{logger} = PerlDaemon::Logger->new($conf);
prestartup $conf;
if ($conf->{'daemon.daemonize'} ne 'yes') {
print "Running in foreground...\n";
} else {
daemonize $conf;
}
sighandlers $conf;
daemonloop $conf;
|