1MooseX::Daemonize::CoreU(s3eprm)Contributed Perl DocumenMtoaotsieoXn::Daemonize::Core(3pm)
2
3
4
6 MooseX::Daemonize::Core - A Role with the core daemonization features
7
9 version 0.22
10
12 package My::Daemon;
13 use Moose;
14
15 with 'MooseX::Daemonize::Core';
16
17 sub start {
18 my $self = shift;
19 # daemonize me ...
20 $self->daemonize;
21 # return from the parent,...
22 return unless $self->is_daemon;
23 # but continue on in the child (daemon)
24 }
25
27 This is the basic daemonization Role, it provides a few methods (see
28 below) and the minimum features needed to properly daemonize your code.
29
30 Important Notes
31 None of the methods in this role will exit the parent process for you,
32 it only forks and detaches your child (daemon) process. It is your
33 responsibility to exit the parent process in some way.
34
35 There is no PID or PID file management in this role, that is your
36 responsibility (see some of the other roles in this distro for that).
37
39 is_daemon (is = rw, isa => Bool)>
40 This attribute is used to signal if we are within the daemon
41 process or not.
42
43 no_double_fork (is = rw, isa => Bool)>
44 Setting this attribute to true will cause this method to not
45 perform the typical double-fork, which is extra added protection
46 from your process accidentally acquiring a controlling terminal.
47 More information can be found above, and by Googling "double fork
48 daemonize".
49
50 If you the double-fork behavior off, you might want to enable the
51 ignore_zombies.
52
53 ignore_zombies (is = rw, isa => Bool)>
54 Setting this attribute to a true value will result in setting the
55 $SIG{CHLD} handler to "IGNORE". This tells perl to clean up zombie
56 processes. By default, and for the most part you don't need it,
57 only when you turn off the double fork behavior (with the
58 no_double_fork attribute) do you sometimes want this behavior.
59
60 dont_close_all_files (is = rw, isa => Bool)>
61 Setting this attribute to true will cause it to skip closing all
62 the filehandles. This is useful if you are opening things like
63 sockets and such in the pre-fork.
64
66 daemon_fork (?%options)
67 This forks off the child process to be daemonized. Just as with the
68 built in fork, it returns the child pid to the parent process, 0 to
69 the child process. It will also set the is_daemon flag
70 appropriately.
71
72 The %options argument remains for backwards compatibility, but it
73 is suggested that you use the attributes listed above instead.
74
75 daemon_detach (?%options)
76 This detaches the new child process from the terminal by doing the
77 following things.
78
79 The %options argument remains for backwards compatibility, but it
80 is suggested that you use the attributes listed above instead.
81
82 Becomes a session leader
83 This detaches the program from the controlling terminal, it is
84 accomplished by calling "POSIX::setsid".
85
86 Performing the double-fork
87 See below for information on how to change this part of the
88 process.
89
90 Changes the current working directory to "/"
91 This is standard daemon behavior, if you want a different
92 working directory then simply change it later in your daemons
93 code.
94
95 Clears the file creation mask.
96 Closes all open file descriptors.
97 See the dont_close_all_files attribute for information on how
98 to change this part of the process.
99
100 Reopen STDERR, STDOUT & STDIN to /dev/null
101 This behavior can be controlled slightly though the
102 "MX_DAEMON_STDERR" and "MX_DAEMON_STDOUT" environment
103 variables. It will look for a filename in either of these
104 variables and redirect "STDOUT" and/or "STDERR" to those files.
105 This is useful for debugging and/or testing purposes.
106
107 NOTE
108
109 If called from within the parent process (the "is_daemon" flag
110 is set to false), this method will simply return and do
111 nothing.
112
113 daemonize (?%options)
114 This will simply call "daemon_fork" followed by
115 "daemon_detach".
116
117 The %options argument remains for backwards compatibility, but
118 it is suggested that you use the attributes listed above
119 instead.
120
121 meta()
122 The "meta()" method from Class::MOP::Class
123
125 Note about double fork
126 Taken from
127 <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012> in
128 a comment entitled The second fork _is_ necessary by Jonathan
129 Bartlett, it is not the definitive statement on the issue, but it's
130 clear and well written enough so I decided to reproduce it here.
131
132 The first fork accomplishes two things - allow the shell to return,
133 and allow you to do a setsid().
134
135 The setsid() removes yourself from your controlling terminal. You
136 see, before, you were still listed as a job of your previous process,
137 and therefore the user might accidentally send you a signal. setsid()
138 gives you a new session, and removes the existing controlling terminal.
139
140 The problem is, you are now a session leader. As a session leader, if
141 you open a file descriptor that is a terminal, it will become your
142 controlling terminal (oops!). Therefore, the second fork makes you NOT
143 be a session leader. Only session leaders can acquire a controlling
144 terminal, so you can open up any file you wish without worrying that
145 it will make you a controlling terminal.
146
147 So - first fork - allow shell to return, and permit you to call setsid()
148
149 Second fork - prevent you from accidentally reacquiring a controlling
150 terminal.
151
152 That said, you don't always want this to be the behavior, so you
153 are free to specify otherwise using the no_double_fork attribute.
154
155 Note about zombies
156 Doing the double fork (see above) tends to get rid of your zombies
157 since by the time you have double forked your daemon process is
158 then owned by the init process. However, sometimes the double-fork
159 is more than you really need, and you want to keep your daemon
160 processes a little closer to you. In this case you have to watch
161 out for zombies, you can avoid then by just setting the
162 ignore_zombies attribute (see above).
163
165 These variables are best just used for debugging and/or testing, but
166 not used for actual logging. For that, you should reopen
167 "STDOUT"/"STDERR" on your own.
168
169 MX_DAEMON_STDOUT
170 A filename to redirect the daemon "STDOUT" to.
171
172 MX_DAEMON_STDERR
173 A filename to redirect the daemon "STDERR" to.
174
176 Moose::Role, POSIX
177
180 Proc::Daemon
181
182 This code is based HEAVILY on Proc::Daemon, we originally depended on
183 it, but we needed some more flexibility, so instead we just stole the
184 code.
185
187 Bugs may be submitted through the RT bug tracker
188 <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Daemonize>
189 (or bug-MooseX-Daemonize@rt.cpan.org <mailto:bug-MooseX-
190 Daemonize@rt.cpan.org>).
191
192 There is also a mailing list available for users of this distribution,
193 at <http://lists.perl.org/list/moose.html>.
194
195 There is also an irc channel available for users of this distribution,
196 at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>.
197
199 • Stevan Little <stevan.little@iinteractive.com>
200
201 • Chris Prather <chris@prather.org>
202
204 This software is copyright (c) 2007 by Chris Prather.
205
206 This is free software; you can redistribute it and/or modify it under
207 the same terms as the Perl 5 programming language system itself.
208
209 Portions heavily borrowed from Proc::Daemon which is copyright Earl
210 Hood.
211
212
213
214perl v5.36.0 2022-07-22 MooseX::Daemonize::Core(3pm)