MooseX...emonize(3pm)
π MooseX::Daemonize β bestie edition π
okay so you want to write a daemon. a persistent one. with a pidfile. that reacts to signals like a well-behaved little icon. this is the MooseX::Daemonize page from the User Contributed Perl Documentation, and I'm gonna walk you through it like we're texting at 2am. grab a boba. π§
β¨ NAME β¨
MooseX::Daemonize β a Role for daemonizing your Moose based application. that's the whole vibe. π¦
πΈ VERSION πΈ
version 0.22 β yes queen, we know the number.
β¨ SYNOPSIS β¨
this is literally the blueprint, copy it and thrive:
package My::Daemon;
use Moose;
with qw(MooseX::Daemonize);
# ... define your class ....
after start => sub {
my $self = shift;
return unless $self->is_daemon;
# your daemon code here ...
};
# then in your script ...
my $daemon = My::Daemon->new_with_options();
my ($command) = @{$daemon->extra_argv}
defined $command || die "No command specified";
$daemon->start if $command eq 'start';
$daemon->status if $command eq 'status';
$daemon->restart if $command eq 'restart';
$daemon->stop if $command eq 'stop';
warn($daemon->status_message);
exit($daemon->exit_code);
no cap, that's it. with qw(MooseX::Daemonize); and suddenly your class has main character energy as a daemon. π
π DESCRIPTION π
so here's the tea: a lot of the time you wanna write a persistent daemon that has a pid file and responds appropriately to Signals. this module hands you a set of basic roles as the infrastructure to do exactly that. it's giving scaffolding, but make it glamorous. β¨
π¨ WARNING π¨
bestie. listen. the maintainers of this module now recommend using Daemon::Control instead. so likeβ¦ it still works, but they're literally telling you to go look at the other one. do with that information what you will. π
π CAVEATS π
okay this part is important so read it slowly.
when it goes into background, MooseX::Daemonize closes all open file handles. and that may interfere with you logging, because it may also close the log file handle you wanna write to. rude, honestly. π
to prevent this you have two options:
- defer opening the log file until after
start, or - use the
'dont_close_all_files'option β either from the command line or in your.shscript.
so say you wanna use Log::Log4perl β for example β you could expand the MooseX::Daemonize example above like this:
after start => sub {
my $self = shift;
return unless $self->is_daemon;
Log::Log4perl->init(\$log4perl_config);
my $logger = Log::Log4perl->get_logger();
$logger->info("Daemon started");
# your daemon code here ...
};
see how the logger gets initialized inside the after start block? that's the whole trick. π
π ATTRIBUTES π
this list includes attributes brought in from other roles as well β we include them here for ease of documentation. all of these attributes are settable through MooseX::Getopt's command line handling, with the exception of is_daemon.
prognamePath::Class::Dir | Strβ the name of our daemon. defaults to"$package_name =~ s/::/_/".pidbasePath::Class::Dir | Strβ the base for our PID. defaults to"/var/run/".basedirPath::Class::Dir | Strβ the directory wechdirto. defaults to"/".pidfileMooseX::Daemonize::Pid::File | Strβ the file we store our PID in. defaults to"$pidbase/$progname.pid".foregroundBoolβ if true, the process won't background. useful for debugging. this option can be set via Getopt's-f.no_double_forkBoolβ if true, the process will not perform the typical double-fork, which is extra added protection from your process accidentally acquiring a controlling terminal. more information can be found by Googling"double fork daemonize".ignore_zombiesBoolβ if true, the process will not clean up zombie processes. normally you don't want this. π§dont_close_all_filesBoolβ if true, the objects open filehandles will not be closed when daemonized. normally you don't want this.is_daemonBoolβ if true, the process is the backgrounded daemon process; if false it is the parent process. this is useful for example in an"after 'start' =" sub { }>block.
NOTE: this option is explicitly not available through MooseX::Getopt.
stop_timeoutβ number of seconds to wait for the process to stop, before trying harder to kill it. defaults to2seconds. π₯
these are the internal attributes, which are not available through MooseX::Getopt:
exit_codeIntstatus_messageStr
β METHODS β
π Daemon Control Methods π
these methods can be used to control the daemon behavior. every effort has been made to have these methods DWIM (Do What I Mean), so that you can focus on just writing the code for your daemon. periodt. π
extending these methods is best done with the Moose method modifiers, such as "before", "after" and "around".
startβ setup a pidfile, fork, then setup the signal handlers.stopβ stop the process matching the pidfile, and unlinks the pidfile.restartβ literally this is:
$self->stop();
$self->start();
statusshutdown
π¦ Pidfile Handling Methods π¦
init_pidfileβ this method will create aMooseX::Daemonize::Pid::Fileobject and tell it to store the PID in the file"$pidbase/$progname.pid".checkβ this checks to see if the daemon process is currently running by checking the pidfile.get_pidβ returns the PID of the daemon process.save_pidβ write the pidfile.remove_pidβ removes the pidfile.
π Signal Handling Methods π
setup_signalsβ setup the signal handlers. by default it only sets up handlers forSIGINTandSIGHUP. if you wish to add more signals just use the"after"method modifier and add them.handle_sigintβ handle aINTsignal, by default calls"$self-"stop()>handle_sighupβ handle aHUPsignal. by default calls"$self-"restart()>
π Exit Code Methods π
these are overridable constant methods used for setting the exit code.
OKβ returns0.ERRORβ returns1.
π«Ά Introspection π«Ά
meta()β the"meta()"method fromClass::MOP::Class
π DEPENDENCIES π
Moose, MooseX::Getopt, MooseX::Types::Path::Class and POSIX. that's the squad. π
π INCOMPATIBILITIES π
obviously this will not work on Windows. (sorry not sorry. β¨)
π₯ SEE ALSO π₯
Daemon::Control, Proc::Daemon, Daemon::Generic. go read those too, iykyk. π
π§ THANKS π§
Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and the #moose denizens. π
some bug fixes sponsored by Takkle Inc.
π SUPPORT π
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>.
πΈ AUTHORS πΈ
Stevan Little<stevan.little@iinteractive.com>Chris Prather<chris@prather.org>
πΈ CONTRIBUTORS πΈ
Karen Etheridge<ether@cpan.org>Michael Reddick<michael.reddick@gmail.com>Yuval Kogman<nothingmuch@woobling.org>Ash Berlin<ash@cpan.org>Brandon L Black<blblack@gmail.com>Jonathan Sailor<jsailor@cpan.org>David Steinbrunner<dsteinbrunner@pobox.com>Michael Schwern<mschwern@cpan.org>Shoichi Kaji<skaji@cpan.org>Dave Rolsky<autarch@urth.org>Chisel Wright<chisel@chizography.net>
π COPYRIGHT AND LICENCE π
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.
okay that's the whole page, bestie. this was rendered by perl v5.30.0 on 2019-12-22, and it's the MooseX...emonize(3pm) page from the User Contributed Perl Documentation. now go forth and daemonize responsibly. ily. π«Άπβ¨
π¦ where this page comes from
- debian bookworm: libmoosex-daemonize-perl 0.22-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-daemonize-perl 0.22-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libmoosex-daemonize-perl 0.22-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-daemonize-perl 0.22-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms