Daemonise

 view release on metacpan or  search on metacpan

lib/Daemonise/Plugin/Daemon.pm  view on Meta::CPAN

            warn "Pid_file created by this same process. Doing nothing.\n";
            return 1;
        }
        else {
            if ($self->phase eq 'status') {
                $self->running($pid);
                return;
            }
            else {
                die
                    "Pid_file already exists for running process ($pid)... aborting\n";
            }
        }
    }
    else {
        ### remove the pid_file

        warn "Pid_file \""
            . $self->pid_file
            . "\" already exists.  Overwriting!\n";
        unlink $self->pid_file
            || die "Couldn't remove pid_file \""
            . $self->pid_file
            . "\" [$!]\n";

        return 1;
    }
}


sub daemonise {
    my ($self) = @_;

    if ($self->foreground) {
        $self->running($$);
        $self->log('staying in foreground');
        return;
    }

    $self->phase('starting');
    $self->check_pid_file if $self->has_pid_file;

    $self->uid($self->_get_uid);
    $self->gid($self->_get_gid);
    $self->gid((split /\s+/, $self->gid)[0]);

    # turn off logging to STDOUT when running in background
    $self->print_log(0);

    my $pid = $self->async;

    ### parent process should do the pid file and exit
    if ($pid) {
        $pid && exit;
        ### child process will continue on
    }
    else {
        $self->_create_pid_file if $self->has_pid_file;

        ### make sure we can remove the file later
        chown($self->uid, $self->gid, $self->pid_file)
            if $self->has_pid_file;

        ### become another user and group
        $self->_set_user;

        ### close all input/output and separate
        ### from the parent process group
        open(STDIN, '<', '/dev/null')
            or die "Can't open STDIN from /dev/null: [$!]";

        $self->stdout_redirect;

        ### Change to root dir to avoid locking a mounted file system
        chdir '/' or die "Can't chdir to \"/\": [$!]";

        ### Turn process into session leader, and ensure no controlling terminal
        POSIX::setsid();

        ### install signal handlers to make sure we shut down gracefully
        $SIG{QUIT} = sub { $self->stop };    ## no critic
        $SIG{TERM} = sub { $self->stop };    ## no critic
        $SIG{INT}  = sub { $self->stop };    ## no critic

        $self->log("daemon started");

        return 1;
    }

    return;
}


around 'stdout_redirect' => sub {
    my ($orig, $self) = @_;

    if ($self->has_logfile) {
        my $logfile = $self->logfile;
        open(STDOUT, '>>', $logfile)
            or die "Can't redirect STDOUT to $logfile: [$!]";
        open(STDERR, '>>', '&STDOUT')
            or die "Can't redirect STDERR to STDOUT: [$!]";
    }
    else {
        open(STDOUT, '>', '/dev/null')
            or die "Can't redirect STDOUT to /dev/null: [$!]";
        open(STDERR, '>', '&STDOUT')
            or die "Can't redirect STDERR to STDOUT: [$!]";
    }

    return;
};


sub status {
    my ($self) = @_;

    $self->phase('status');
    $self->check_pid_file();

    if ($self->is_running) {



( run in 0.548 second using v1.01-cache-2.11-cpan-71847e10f99 )