NAME
    Daemon::Daemonize - A daemonizer

VERSION
    Version 0.002

SYNOPSIS
        use Daemon::Daemonize qw/ :all /

        daemonize( %options, run => sub {

            # Daemon code in here...

        } )

        # Do some non-daemon stuff here...

    You can also use it in the traditional way, daemonizing the current
    process:

        daemonize( %options )

        # Daemon code in here...

    and use it to check up on your daemon:

        # In your daemon

        use Daemon::Daemonize qw/ :all /

        write_pidfile( $pidfile )
        $SIG{INT} = sub { delete_pidfile( $pidfile ) }

        ... Elsewhere ...

        use Daemon::Daemonize qw/ :all /

        # Return the pid from $pidfile if it contains a pid AND
        # the process is running (even if you don't own it), 0 otherwise
        my $pid = check_pidfile( $pidfile )

        # Return the pid from $pidfile, or undef if the
        # file doesn't exist, is unreadable, etc.
        # This will return the pid regardless of if the process is running
        my $pid = read_pidfile( $pidfile )

DESCRIPTION
    Daemon::Daemonize is a toolbox for daemonizing processes and checking up
    on them. It takes inspiration from
    <http://www.clapper.org/software/daemonize/>, MooseX::Daemon,
    Net::Server::Daemon, and more...

    Being new, the API is currently fluid, but shouldn't change too much

  A note about "close => std"
    If you're having trouble with IPC in a daemon, try closing only STD*
    instead of everything. This is a workaround for a problem with using
    "Net::Server" and "IPC::Open3" in a daemonized process

USAGE
    You can use the following in two ways, either importing them:

        use Daemon::Daemonize qw/ daemonize /

        daemonize( ... )

    or calling them as a class method:

        use Daemon::Daemonize

        Daemon::Daemonize->daemonize

  daemonize( %options )
    Daemonize via the current process, according to %options:

        chdir <dir>         Change to <dir> when daemonizing. Pass undef for *no* chdir.
                            Default is '/' (for avoiding umount difficulty)

        close <option>      Automatically close opened files when daemonizing:

                                1     Close STDIN, STDOUT, STDERR (usually
                                      redirected from/to /dev/null). In addition, close
                                      any other opened files (up to POSIX::_SC_OPEN_MAX)

                                0     No closing

                                std   Only close STD{IN,OUT,ERR} (as in 1)

                            Default is 1

        stdout <file>       Open up STDOUT of the process to <file>. This will override no_close

        stderr <file>       Open up STDERR of the process to <file>. This will override no_close

        run <code>          After daemonizing, run the given code and then exit

  read_pidfile( $pidfile )
    Return the pid from $pidfile. Return undef if the file doesn't exist, is
    unreadable, etc. This will return the pid regardless of if the process
    is running

    For an alternative, see "check_pidfile"

  write_pidfile( $pidfile, [ $pid ] )
    Write the given pid to $pidfile, creating/overwriting any existing file.
    The second argument is optional, and will default to $$ (the current
    process number)

  delete_pidfile( $pidfile )
    Unconditionally delete (unlink) $pidfile

  check_pidfile( $pidfile )
    Return the pid from $pidfile if it contains a pid AND the process is
    running (even if you don't own it), and 0 otherwise

    This method will always return a number

SEE ALSO
    MooseX::Daemonize

    Proc::Daemon

    Net::Server::Daemonize

AUTHOR
    Robert Krimen, "<rkrimen at cpan.org>"

BUGS
    Please report any bugs or feature requests to "bug-daemon-daemonize at
    rt.cpan.org", or through the web interface at
    <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Daemon-Daemonize>. I
    will be notified, and then you'll automatically be notified of progress
    on your bug as I make changes.

SUPPORT
    You can find documentation for this module with the perldoc command.

        perldoc Daemon::Daemonize

    You can also look for information at:

    *   RT: CPAN's request tracker

        <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Daemon-Daemonize>

    *   AnnoCPAN: Annotated CPAN documentation

        <http://annocpan.org/dist/Daemon-Daemonize>

    *   CPAN Ratings

        <http://cpanratings.perl.org/d/Daemon-Daemonize>

    *   Search CPAN

        <http://search.cpan.org/dist/Daemon-Daemonize/>

ACKNOWLEDGEMENTS
COPYRIGHT & LICENSE
    Copyright 2009 Robert Krimen.

    This program is free software; you can redistribute it and/or modify it
    under the terms of either: the GNU General Public License as published
    by the Free Software Foundation; or the Artistic License.

    See http://dev.perl.org/licenses/ for more information.

