JIP-Daemon
view release on metacpan or search on metacpan
lib/JIP/Daemon.pm view on Meta::CPAN
sub status {
my $self = shift;
my $pid = $self->pid;
return $pid, POSIX::kill($pid, 0) ? 1 : 0, $self->is_detached;
}
sub change_program_name {
my $self = shift;
my $old_program_name = $PROGRAM_NAME;
my $new_program_name = $self->program_name;
if ($new_program_name ne $old_program_name) {
$self->_log(
'The program name changed from %s to %s',
$old_program_name,
$new_program_name,
);
$PROGRAM_NAME = $new_program_name;
}
return $self;
}
# private methods
sub _log {
my $self = shift;
$self->log_callback->($self, @ARG);
return $self;
}
1;
__END__
=head1 NAME
JIP::Daemon - Daemonize server process.
=head1 VERSION
This document describes C<JIP::Daemon> version C<0.041>.
=head1 SYNOPSIS
Just run:
use JIP::Daemon;
my $proc = JIP::Daemon->new;
# Send program to backgroung.
$proc = $proc->daemonize;
# In the backgroung process:
$proc->is_detached; # 1
$proc->try_kill(0); # 1
printf qq{pid(%s), is_alive(%d), is_detached(%d)\n}, $proc->status;
# If the program is already a running background job, the daemonize method shall have no effect.
$proc = $proc->daemonize;
Dry run:
use JIP::Daemon;
my $proc = JIP::Daemon->new(dry_run => 1);
$proc->daemonize;
# In the same process
$proc->is_detached; # 0
With logger:
use Mojo::Log;
use JIP::Daemon;
my $proc = JIP::Daemon->new(logger => Mojo::Log->new);
$proc->daemonize;
With on_fork_callback:
use JIP::Daemon;
my $proc = JIP::Daemon->new(
on_fork_callback => sub {
# After daemonizing, and before exiting,
# run the given code in parent process
print $proc->pid;
},
)->daemonize;
=head1 ATTRIBUTES
C<JIP::Daemon> implements the following attributes.
=head2 pid
my $pid = $proc->pid;
=head2 is_detached
my $is_detached => $proc->is_detached;
Process is detached from the controlling terminal.
=head2 dry_run
my $dry_run = $proc->dry_run;
Perform a trial run with no changes made (foreground if dry_run).
=head2 uid
my $uid = $proc->uid;
lib/JIP/Daemon.pm view on Meta::CPAN
Returns a string representation of the null device.
=head2 stdout
my $stdout = $proc->stdout;
If this parameter is supplied, redirect STDOUT to file.
=head2 stderr
my $stderr = $proc->stderr;
If this parameter is supplied, redirect STDERR to file.
=head2 program_name
my $program_name = $proc->program_name;
Returns a string with name of the process.
=head1 SUBROUTINES/METHODS
=head2 new
my $proc = JIP::Daemon->new;
my $proc = JIP::Daemon->new(dry_run => 1);
Construct a new C<JIP::Daemon> object.
=head2 daemonize
$proc = $proc->daemonize;
Daemonize server process.
=head2 reopen_std
$proc = $proc->reopen_std;
Reopen STDIN, STDOUT, STDERR to /dev/null.
my $proc = JIP::Daemon->new(
stdout => '+>/path/to/out.log',
stderr => '+>/path/to/err.log',
);
$proc = $proc->reopen_std;
The C<stdout> and C<stderr> arguments are file names that will be opened and be used to replace the standard file descriptors. These special modes only work with two-argument form of C<open>.
=head2 drop_privileges
my $proc = JIP::Daemon->new(uid => 1000, gid => 1000, umask => 0, cwd => q{/});
$proc = $proc->drop_privileges;
Change C<uid>/C<gid>/C<umask>/C<cwd> on demand.
=head2 try_kill
my $is_alive = $proc->try_kill(0);
This is identical to Perl's builtin C<kill()> function for sending signals to processes (often to terminate them).
=head2 change_program_name
my $proc = JIP::Daemon->new(program_name => 'tratata');
$proc = $proc->change_program_name;
Changes the name of the program.
=head2 status
my ($pid, $is_alive, $is_detached) = $proc->status;
Returns a list of process attributes: PID, is alive, is detached (in backgroung).
=head1 DIAGNOSTICS
None.
=head1 CONFIGURATION AND ENVIRONMENT
C<JIP::Daemon> requires no configuration files or environment variables.
=head1 SEE ALSO
L<POSIX>, L<Privileges::Drop>, L<Object::ForkAware>, L<Proc::Daemon>, L<Daemon::Daemonize>.
=head1 AUTHOR
Vladimir Zhavoronkov, C<< <flyweight at yandex.ru> >>
=head1 LICENSE AND COPYRIGHT
Copyright 2015-2018 Vladimir Zhavoronkov.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
( run in 1.969 second using v1.01-cache-2.11-cpan-39bf76dae61 )