MooseX::Daemonize::Core(3pm) | User Contributed Perl Documentation | MooseX::Daemonize::Core(3pm) |
MooseX::Daemonize::Core - A Role with the core daemonization features
version 0.22
package My::Daemon; use Moose; with 'MooseX::Daemonize::Core'; sub start { my $self = shift; # daemonize me ... $self->daemonize; # return from the parent,... return unless $self->is_daemon; # but continue on in the child (daemon) }
This is the basic daemonization Role, it provides a few methods (see below) and the minimum features needed to properly daemonize your code.
None of the methods in this role will exit the parent process for you, it only forks and detaches your child (daemon) process. It is your responsibility to exit the parent process in some way.
There is no PID or PID file management in this role, that is your responsibility (see some of the other roles in this distro for that).
If you the double-fork behavior off, you might want to enable the ignore_zombies.
The %options argument remains for backwards compatibility, but it is suggested that you use the attributes listed above instead.
The %options argument remains for backwards compatibility, but it is suggested that you use the attributes listed above instead.
NOTE
If called from within the parent process (the "is_daemon" flag is set to false), this method will simply return and do nothing.
The %options argument remains for backwards compatibility, but it is suggested that you use the attributes listed above instead.
The first fork accomplishes two things - allow the shell to return, and allow you to do a setsid(). The setsid() removes yourself from your controlling terminal. You see, before, you were still listed as a job of your previous process, and therefore the user might accidentally send you a signal. setsid() gives you a new session, and removes the existing controlling terminal. The problem is, you are now a session leader. As a session leader, if you open a file descriptor that is a terminal, it will become your controlling terminal (oops!). Therefore, the second fork makes you NOT be a session leader. Only session leaders can acquire a controlling terminal, so you can open up any file you wish without worrying that it will make you a controlling terminal. So - first fork - allow shell to return, and permit you to call setsid() Second fork - prevent you from accidentally reacquiring a controlling terminal.
That said, you don't always want this to be the behavior, so you are free to specify otherwise using the no_double_fork attribute.
These variables are best just used for debugging and/or testing, but not used for actual logging. For that, you should reopen "STDOUT"/"STDERR" on your own.
Moose::Role, POSIX
Proc::Daemon
This code is based HEAVILY on Proc::Daemon, we originally depended on it, but we needed some more flexibility, so instead we just stole the code.
Bugs may be submitted through the RT bug tracker <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Daemonize> (or bug-MooseX-Daemonize@rt.cpan.org <mailto:bug-MooseX-Daemonize@rt.cpan.org>).
There is also a mailing list available for users of this distribution, at <http://lists.perl.org/list/moose.html>.
There is also an irc channel available for users of this distribution, at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>.
This software is copyright (c) 2007 by Chris Prather.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Portions heavily borrowed from Proc::Daemon which is copyright Earl Hood.
2019-12-22 | perl v5.30.0 |