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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Having fun with computers!</title>
<link rel="shortcut icon" type="image/gif" href="/favicon.ico" />
<style type="text/css">
body {
margin: auto;
max-width: 900px;
background-color: #FFFFEF;
border: 1px dashed #880000;
border-radius: 8px;
padding: 5px;
}
img {
display:block;
max-width: 80%;
}
p.quote:before {
content: " | ";
padding-left: 2px;
}
a.textlink:before {
content: " ⇒ ";
padding-left: 2px;
}
a.textlink {
text-decoration: none;
color: #FF0000;
}
a.textlink:hover {
text-decoration: underline;
}
pre {
background-color: #F1F8E9;
border: 1px dashed #BB0000;
border-radius: 8px;
padding: 5px;
font-family: "Lucida Console", "Courier New", monospace;
}
h1 {
text-align: center;
color: #880000;
}
h2, h3 {
color: #BB0000;
}
</style>
</head>
<body>
<h1>Perl Daemon (Service Framework)</h1>
<pre>
a'! _,,_ a'! _,,_ a'! _,,_
\\_/ \ \\_/ \ \\_/ \.-,
\, /-( /'-,\, /-( /'-, \, /-( /
//\ //\\ //\ //\\ //\ //\\jrei
</pre>
<p class="quote"><i>Written by Paul Buetow 2011-05-07, last updated 2021-05-07</i></p>
<p>PerlDaemon is a minimal daemon for Linux and other Unix like operating systems programmed in Perl. It is a minimal but pretty functional and fairly generic service framework. This means that it does not do anything useful other than providing a framework for starting, stopping, configuring and logging. In order to do something useful, a module (written in Perl) must be provided.</p>
<h2>Features</h2>
<p>PerlDaemon supports:</p>
<ul>
<li>Automatic daemonizing</li>
<li>Logging</li>
<li>logrotation (via SIGHUP)</li>
<li>Clean shutdown support (SIGTERM)</li>
<li>Pid file support (incl. check on startup)</li>
<li>Easy to configure</li>
<li>Easy to extend</li>
<li>Multi instance support (just use a different directory for each instance).</li>
</ul>
<h2>Quick Guide</h2>
<pre>
# Starting
./bin/perldaemon start (or shortcut ./control start)
# Stopping
./bin/perldaemon stop (or shortcut ./control stop)
# Alternatively: Starting in foreground
./bin/perldaemon start daemon.daemonize=no (or shortcut ./control foreground)
</pre>
<p>To stop a daemon running in foreground mode "Ctrl+C" must be hit. To see more available startup options run "./control" without any argument.</p>
<h2>How to configure</h2>
<p>The daemon instance can be configured in "./conf/perldaemon.conf". If you want to change a property only once, it is also possible to specify it on command line (that then will take precedence over the config file). All available config properties can be viewed via "./control keys":</p>
<pre>
pb@titania:~/svn/utils/perldaemon/trunk$ ./control keys
# Path to the logfile
daemon.logfile=./log/perldaemon.log
# The amount of seconds until the next event look takes place
daemon.loopinterval=1
# Path to the modules dir
daemon.modules.dir=./lib/PerlDaemonModules
# Specifies either the daemon should run in daemon or foreground mode
daemon.daemonize=yes
# Path to the pidfile
daemon.pidfile=./run/perldaemon.pid
# Each module should run every runinterval seconds
daemon.modules.runinterval=3
# Path to the alive file (is touched every loopinterval seconds, usable to monitor)
daemon.alivefile=./run/perldaemon.alive
# Specifies the working directory
daemon.wd=./
</pre>
<h2>Example </h2>
<p>So lets start the daemon with a loop interval of 10 seconds:</p>
<pre>
$ ./control keys | grep daemon.loopinterval
daemon.loopinterval=1
$ ./control keys daemon.loopinterval=10 | grep daemon.loopinterval
daemon.loopinterval=10
$ ./control start daemon.loopinterval=10; sleep 10; tail -n 2 log/perldaemon.log
Starting daemon now...
Mon Jun 13 11:29:27 2011 (PID 2838): Triggering PerlDaemonModules::ExampleModule
(last triggered before 10.002106s; carry: 7.002106s; wanted interval: 3s)
Mon Jun 13 11:29:27 2011 (PID 2838): ExampleModule Test 2
$ ./control stop
Stopping daemon now...
</pre>
<p>If you want to change that property forever either edit perldaemon.conf or do this:</p>
<pre>
$ ./control keys daemon.loopinterval=10 > new.conf; mv new.conf conf/perldaemon.conf
</pre>
<h2>HiRes event loop</h2>
<p>PerlDaemon uses `Time::HiRes` to make sure that all the events run in correct intervals. Each loop run a time carry value is recorded and added to the next loop run in order to catch up lost time.</p>
<h2>Writing your own modules</h2>
<h3>Example module</h3>
<p>This is one of the example modules you will find in the source code. It should be quite self-explanatory if you know Perl :-).</p>
<pre>
package PerlDaemonModules::ExampleModule;
use strict;
use warnings;
sub new ($$$) {
my ($class, $conf) = @_;
my $self = bless { conf => $conf }, $class;
# Store some private module stuff
$self->{counter} = 0;
return $self;
}
# Runs periodically in a loop (set interval in perldaemon.conf)
sub do ($) {
my $self = shift;
my $conf = $self->{conf};
my $logger = $conf->{logger};
# Calculate some private module stuff
my $count = ++$self->{counter};
$logger->logmsg("ExampleModule Test $count");
}
1;
</pre>
<h3>Your own module</h3>
<p>Want to give it some better use? It's just a easy as:</p>
<pre>
cd ./lib/PerlDaemonModules/
cp ExampleModule.pm YourModule.pm
vi YourModule.pm
cd -
./bin/perldaemon restart (or shortcurt ./control restart)
</pre>
<p>Now watch `./log/perldaemon.log` closely. It is a good practise to test your modules in 'foreground mode' (see above how to do that).</p>
<p>BTW: You can install as many modules within the same instance as desired. But they are run in sequential order (in future they can also run in parallel using several threads or processes).</p>
<h2>May the source be with you</h2>
<p>You can find PerlDaemon (including the examples) at:</p>
<a class="textlink" href="https://github.com/snonux/perldaemon">https://github.com/snonux/perldaemon</a><br />
<p>E-Mail me your thoughts at comments@mx.buetow.org!</p>
<a class="textlink" href="../">Go back to the main site</a><br />
</body>
</html>
|