The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


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 ...
      # 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.

Important Notes

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).


is_daemon (is = rw, isa => Bool)>

This attribute is used to signal if we are within the daemon process or not.

no_double_fork (is = rw, isa => Bool)>

Setting this attribute to true will cause this method to not perform the typical double-fork, which is extra added protection from your process accidentally acquiring a controlling terminal. More information can be found above, and by Googling "double fork daemonize".

If you the double-fork behavior off, you might want to enable the ignore_zombies.

ignore_zombies (is = rw, isa => Bool)>

Setting this attribute to a true value will result in setting the $SIG{CHLD} handler to IGNORE. This tells perl to clean up zombie processes. By default, and for the most part you don't need it, only when you turn off the double fork behavior (with the no_double_fork attribute) do you sometimes want this behavior.

dont_close_all_files (is = rw, isa => Bool)>

Setting this attribute to true will cause it to skip closing all the filehandles. This is useful if you are opening things like sockets and such in the pre-fork.


daemon_fork (?%options)

This forks off the child process to be daemonized. Just as with the built in fork, it returns the child pid to the parent process, 0 to the child process. It will also set the is_daemon flag appropriately.

The %options argument remains for backwards compatibility, but it is suggested that you use the attributes listed above instead.

daemon_detach (?%options)

This detaches the new child process from the terminal by doing the following things.

The %options argument remains for backwards compatibility, but it is suggested that you use the attributes listed above instead.

Becomes a session leader

This detaches the program from the controlling terminal, it is accomplished by calling POSIX::setsid.

Performing the double-fork

See below for information on how to change this part of the process.

Changes the current working directory to "/"

This is standard daemon behavior, if you want a different working directory then simply change it later in your daemons code.

Clears the file creation mask.
Closes all open file descriptors.

See the dont_close_all_files attribute for information on how to change this part of the process.

Reopen STDERR, STDOUT & STDIN to /dev/null

This behavior can be controlled slightly though the MX_DAEMON_STDERR and MX_DAEMON_STDOUT environment variables. It will look for a filename in either of these variables and redirect STDOUT and/or STDERR to those files. This is useful for debugging and/or testing purposes.


If called from within the parent process (the is_daemon flag is set to false), this method will simply return and do nothing.

daemonize (?%options)

This will simply call daemon_fork followed by daemon_detach.

The %options argument remains for backwards compatibility, but it is suggested that you use the attributes listed above instead.


The meta() method from Class::MOP::Class


Note about double fork

Taken from in a comment entitled The second fork _is_ necessary by Jonathan Bartlett, it is not the definitive statement on the issue, but it's clear and well written enough so I decided to reproduce it here.

  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

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.

Note about zombies

Doing the double fork (see above) tends to get rid of your zombies since by the time you have double forked your daemon process is then owned by the init process. However, sometimes the double-fork is more than you really need, and you want to keep your daemon processes a little closer to you. In this case you have to watch out for zombies, you can avoid then by just setting the ignore_zombies attribute (see above).


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.


A filename to redirect the daemon STDOUT to.


A filename to redirect the daemon STDERR to.


Moose::Role, POSIX




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 (or

There is also a mailing list available for users of this distribution, at

There is also an irc channel available for users of this distribution, at #moose on


  • Stevan Little <>

  • Chris Prather <>


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.