1MooseX::Daemonize(3pm)User Contributed Perl DocumentationMooseX::Daemonize(3pm)
2
3
4
6 MooseX::Daemonize - Role for daemonizing your Moose based application
7
9 version 0.22
10
12 package My::Daemon;
13 use Moose;
14
15 with qw(MooseX::Daemonize);
16
17 # ... define your class ....
18
19 after start => sub {
20 my $self = shift;
21 return unless $self->is_daemon;
22 # your daemon code here ...
23 };
24
25 # then in your script ...
26
27 my $daemon = My::Daemon->new_with_options();
28
29 my ($command) = @{$daemon->extra_argv}
30 defined $command || die "No command specified";
31
32 $daemon->start if $command eq 'start';
33 $daemon->status if $command eq 'status';
34 $daemon->restart if $command eq 'restart';
35 $daemon->stop if $command eq 'stop';
36
37 warn($daemon->status_message);
38 exit($daemon->exit_code);
39
41 Often you want to write a persistent daemon that has a pid file, and
42 responds appropriately to Signals. This module provides a set of basic
43 roles as an infrastructure to do that.
44
46 The maintainers of this module now recommend using Daemon::Control
47 instead.
48
50 When going into background MooseX::Daemonize closes all open file
51 handles. This may interfere with you logging because it may also close
52 the log file handle you want to write to. To prevent this you can
53 either defer opening the log file until after start. Alternatively, use
54 can use the 'dont_close_all_files' option either from the command line
55 or in your .sh script.
56
57 Assuming you want to use Log::Log4perl for example you could expand the
58 MooseX::Daemonize example above like this.
59
60 after start => sub {
61 my $self = shift;
62 return unless $self->is_daemon;
63 Log::Log4perl->init(\$log4perl_config);
64 my $logger = Log::Log4perl->get_logger();
65 $logger->info("Daemon started");
66 # your daemon code here ...
67 };
68
70 This list includes attributes brought in from other roles as well we
71 include them here for ease of documentation. All of these attributes
72 are settable though MooseX::Getopt's command line handling, with the
73 exception of "is_daemon".
74
75 progname Path::Class::Dir | Str
76 The name of our daemon, defaults to "$package_name =~ s/::/_/";
77
78 pidbase Path::Class::Dir | Str
79 The base for our PID, defaults to "/var/run/"
80
81 basedir Path::Class::Dir | Str
82 The directory we chdir to; defaults to "/".
83
84 pidfile MooseX::Daemonize::Pid::File | Str
85 The file we store our PID in, defaults to "$pidbase/$progname.pid"
86
87 foreground Bool
88 If true, the process won't background. Useful for debugging. This
89 option can be set via Getopt's -f.
90
91 no_double_fork Bool
92 If true, the process will not perform the typical double-fork,
93 which is extra added protection from your process accidentally
94 acquiring a controlling terminal. More information can be found by
95 Googling "double fork daemonize".
96
97 ignore_zombies Bool
98 If true, the process will not clean up zombie processes. Normally
99 you don't want this.
100
101 dont_close_all_files Bool
102 If true, the objects open filehandles will not be closed when
103 daemonized. Normally you don't want this.
104
105 is_daemon Bool
106 If true, the process is the backgrounded daemon process, if false
107 it is the parent process. This is useful for example in an "after
108 'start' =" sub { }> block.
109
110 NOTE: This option is explicitly not available through
111 MooseX::Getopt.
112
113 stop_timeout
114 Number of seconds to wait for the process to stop, before trying
115 harder to kill it. Defaults to 2 seconds.
116
117 These are the internal attributes, which are not available through
118 MooseX::Getopt.
119
120 exit_code Int
121 status_message Str
122
124 Daemon Control Methods
125 These methods can be used to control the daemon behavior. Every effort
126 has been made to have these methods DWIM (Do What I Mean), so that you
127 can focus on just writing the code for your daemon.
128
129 Extending these methods is best done with the Moose method modifiers,
130 such as "before", "after" and "around".
131
132 start
133 Setup a pidfile, fork, then setup the signal handlers.
134
135 stop
136 Stop the process matching the pidfile, and unlinks the pidfile.
137
138 restart
139 Literally this is:
140
141 $self->stop();
142 $self->start();
143
144 status
145 shutdown
146
147 Pidfile Handling Methods
148 init_pidfile
149 This method will create a MooseX::Daemonize::Pid::File object and
150 tell it to store the PID in the file "$pidbase/$progname.pid".
151
152 check
153 This checks to see if the daemon process is currently running by
154 checking the pidfile.
155
156 get_pid
157 Returns the PID of the daemon process.
158
159 save_pid
160 Write the pidfile.
161
162 remove_pid
163 Removes the pidfile.
164
165 Signal Handling Methods
166 setup_signals
167 Setup the signal handlers, by default it only sets up handlers for
168 SIGINT and SIGHUP. If you wish to add more signals just use the
169 "after" method modifier and add them.
170
171 handle_sigint
172 Handle a INT signal, by default calls "$self-"stop()>
173
174 handle_sighup
175 Handle a HUP signal. By default calls "$self-"restart()>
176
177 Exit Code Methods
178 These are overridable constant methods used for setting the exit code.
179
180 OK Returns 0.
181
182 ERROR
183 Returns 1.
184
185 Introspection
186 meta()
187 The "meta()" method from Class::MOP::Class
188
190 Moose, MooseX::Getopt, MooseX::Types::Path::Class and POSIX
191
193 Obviously this will not work on Windows.
194
196 Daemon::Control, Proc::Daemon, Daemon::Generic
197
199 Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and
200 the #moose denizens
201
202 Some bug fixes sponsored by Takkle Inc.
203
205 Bugs may be submitted through the RT bug tracker
206 <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Daemonize>
207 (or bug-MooseX-Daemonize@rt.cpan.org <mailto:bug-MooseX-
208 Daemonize@rt.cpan.org>).
209
210 There is also a mailing list available for users of this distribution,
211 at <http://lists.perl.org/list/moose.html>.
212
213 There is also an irc channel available for users of this distribution,
214 at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>.
215
217 · Stevan Little <stevan.little@iinteractive.com>
218
219 · Chris Prather <chris@prather.org>
220
222 · Karen Etheridge <ether@cpan.org>
223
224 · Michael Reddick <michael.reddick@gmail.com>
225
226 · Yuval Kogman <nothingmuch@woobling.org>
227
228 · Ash Berlin <ash@cpan.org>
229
230 · Brandon L Black <blblack@gmail.com>
231
232 · Jonathan Sailor <jsailor@cpan.org>
233
234 · David Steinbrunner <dsteinbrunner@pobox.com>
235
236 · Michael Schwern <mschwern@cpan.org>
237
238 · Shoichi Kaji <skaji@cpan.org>
239
240 · Dave Rolsky <autarch@urth.org>
241
242 · Chisel Wright <chisel@chizography.net>
243
245 This software is copyright (c) 2007 by Chris Prather.
246
247 This is free software; you can redistribute it and/or modify it under
248 the same terms as the Perl 5 programming language system itself.
249
250
251
252perl v5.32.0 2020-07-28 MooseX::Daemonize(3pm)