Commit 08f292ee authored by Xavier Oswald's avatar Xavier Oswald

[svn-inject] Installing original source of libapp-daemon-perl

parents
blib
pm_to_blib
Makefile
adm
Revision history for Perl extension App::Daemon.
0.06 (03/05/2009)
(ms) Added detach() as an importable method for simple daemons.
0.05 (02/17/2009)
(ms) Better docs as requested by Tim Appnel
(ms) docs on application-specific command line options
(ms) Better daemonization according to Stevens (Advanced Programming
in the UNIX environment)
(ms) Make sure the child isn't killed by a closed session before
it is able to detach from the tty.
(ms) Better __DIE__ handler as suggested by Karl Rune Nilsen in
http://rt.cpan.org/Ticket/Display.html?id=39917.
0.04 10/02/2008
(ms) Fixed logfile permissions if daemon starts up at root and
then drops priviledges. Added docs.
0.03 08/03/2008
(ms) Fixed test suite for Freebsd, which locks tempfiles
exclusively. Thanks to to CPAN tester Slaven Rezic.
(ms) Removed restriction on perl-5.8.
0.02 08/01/2008
(ms) Added setting logfile and pidfile within the script itself, as
suggested by Kimo Rosenbaum.
(ms) Fixed OLDERR warnings in test suite.
0.01 07/19/2008
(ms) Where it all began.
This diff is collapsed.
.cvsignore
adm/release
Changes
Daemon.pm
eg/test-daemon
eg/test-detach
Makefile.PL
MANIFEST
README
t/001Basic.t
t/002Params.t
META.yml Module meta-data (added by MakeMaker)
--- #YAML:1.0
name: App-Daemon
version: 0.06
abstract: Start an Application as a Daemon
license: ~
generated_by: ExtUtils::MakeMaker version 6.31
distribution_type: module
requires:
File::Basename: 0
File::Pid: 0
File::Temp: 0
Getopt::Std: 0
Log::Log4perl: 1.0
Pod::Usage: 0
Proc::ProcessTable: 0
Test::More: 0
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.2.html
version: 1.2
author:
- Mike Schilli <m@perlmeister.com>
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
NAME => 'App::Daemon',
VERSION_FROM => 'Daemon.pm', # finds $VERSION
PREREQ_PM => {
Log::Log4perl => "1.0",
File::Pid => 0,
Proc::ProcessTable => 0,
Getopt::Std => 0,
Pod::Usage => 0,
File::Basename => 0,
Test::More => 0,
File::Temp => 0,
}, # e.g., Module::Name => 1.1
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT_FROM => 'Daemon.pm', # retrieve abstract from module
AUTHOR => 'Mike Schilli <m@perlmeister.com>') : ()),
);
######################################################################
App::Daemon 0.06
######################################################################
NAME
App::Daemon - Start an Application as a Daemon
SYNOPSIS
# Program:
use App::Daemon qw( daemonize );
daemonize();
do_something_useful(); # your application
# Then, in the shell: start application,
# which returns immediately, but continues
# to run do_something_useful() in the background
$ app start
$
# stop application
$ app stop
# start app in foreground (for testing)
$ app -X
# show if app is currently running
$ app status
DESCRIPTION
"App::Daemon" helps running an application as a daemon. The idea is that
you prepend your script with the
use App::Daemon qw( daemonize );
daemonize();
and 'daemonize' it that way. That means, that if you write
use App::Daemon qw( daemonize );
daemonize();
sleep(10);
you'll get a script that, when called from the command line, returns
immediatly, but continues to run as a daemon for 10 seconds.
Along with the common features offered by similar modules on CPAN, it
* supports logging with Log4perl: In background mode, it logs to a
logfile. In foreground mode, log messages go directly to the screen.
* detects if another instance is already running and ends itself
automatically in this case.
* shows with the 'status' command if an instance is already running
and which PID it has:
./my-app status
Pid file: /tmp/tt.pid
Pid in file: 14914
Running: no
Name match: 0
Actions
"App::Daemon" recognizes three different actions:
my-app start
will start up the daemon. "start" itself is optional, as this is the
default action,
$ ./my-app
will also run the 'start' action. If the -X option is given, the
program is run in foreground mode for testing purposes.
stop
will find the daemon's PID in the pidfile and send it a kill signal.
It won't verify if this actually shut down the daemon or if it's
immune to the kill signal.
status
will print out diagnostics on what the status of the daemon is.
Typically, the output look like this:
Pid file: /tmp/tt.pid
Pid in file: 15562
Running: yes
Name match: 1
/usr/local/bin/perl -w test.pl
This indicates that the pidfile says that the daemon has PID 15562
and that a process with this PID is actually running at this moment.
Also, a name grep on the process name in the process table results
in 1 match, according to the output above.
Note that the name match is unreliable, as it just looks for a
command line that looks approximately like the script itself. So if
the script is "test.pl", it will match lines like "perl -w test.pl"
or "perl test.pl start", but unfortunately also lines like "vi
test.pl".
If the process is no longer running, the status output might look
like this instead:
Pid file: /tmp/tt.pid
Pid in file: 14914
Running: no
Name match: 0
Command Line Options
-X Foreground mode. Log messages go to the screen.
-l logfile
Logfile to send Log4perl messages to in background mode.
Defaults to "/tmp/[appname].log".
-u as_user
User to run as if started as root. Defaults to 'nobody'.
-l4p l4p.conf
Path to Log4perl configuration file.
-p pidfile
Where to save the pid of the started process. Defaults to
"/tmp/[appname].pid".
Setting Parameters
Instead of setting paramteters like the logfile, the pidfile
etc. from the command line, you can directly manipulate
App::Daemon's global variables:
use App::Daemon qw(daemonize);
$App::Daemon::logfile = "mylog.log";
$App::Daemon::pidfile = "mypid.log";
$App::Daemon::l4p_conf = "myconf.l4p";
$App::Daemon::background = 1;
$App::Daemon::as_user = "nobody";
use Log::Log4perl qw(:levels);
$App::Daemon::loglevel = $DEBUG;
daemonize();
Application-specific command line options
If an application needs additional command line options, it can
use whatever is not yet taken by App::Daemon, as described
previously in the "Command Line Options" section.
However, it needs to make sure to remove these additional
options before calling daemonize(), or App::Daemon will
complain. To do this, create an options hash %opts and store
application-specific options in there while removing them from
@ARGV:
my %opts = ();
for my $opt (qw(k P U)) {
my $v = App::Daemon::find_option( $opt, 1 );
$opts{ $opt } = $v if defined $v;
}
After this, options "-k", "-P", and "-U" will have disappeared
from @ARGV and can be checked in $opts{k}, $opts{P}, and
$opts{U}.
Gotchas
If the process is started as root but later drops permissions to
a non-priviledged user for security purposes, it's important
that logfiles are created with correct permissions.
If they're created as root when the program starts, the
non-priviledged user won't be able to write to them later
(unless they're world-writable which is also undesirable because
of security concerns).
The best strategy to handle this case is to specify the
non-priviledged user as the owner of the logfile in the Log4perl
configuration:
log4perl.logger = DEBUG, FileApp
log4perl.appender.FileApp = Log::Log4perl::Appender::File
log4perl.appender.FileApp.filename = /var/log/foo-app.log
log4perl.appender.FileApp.owner = nobody
log4perl.appender.FileApp.layout = PatternLayout
log4perl.appender.FileApp.layout.ConversionPattern = %d %m%n
This way, the process starts up as root, creates the logfile if
it doesn't exist yet, and changes its owner to 'nobody'. Later,
when the process assumes the identity of the user 'nobody', it
will continue to write to the logfile without permission
problems.
Detach only
If you want to create a daemon without the fancy command line
parsing and PID file checking functions, use
use App::Daemon qw(detach);
detach();
# ... some code here
This will fork a child, terminate the parent and detach the
child from the terminal. Issued from the command line, the
program above will continue to run the code following the
detach() call but return to the shell prompt immediately.
AUTHOR
Mike Schilli, cpan@perlmeister.com
COPYRIGHT AND LICENSE
Copyright (C) 2008 by Mike Schilli
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5
or, at your option, any later version of Perl 5 you may have
available.
#!/usr/local/bin/perl
use warnings;
use strict;
# Available at http://perlmeister.com/scripts
use lib "$ENV{HOME}/perl-modules";
use ModDevUtils;
ModDevUtils::release(0) or exit 0;
#!/usr/local/bin/perl -w
use strict;
# Program:
use App::Daemon qw( daemonize );
daemonize();
sleep(100);
#!/usr/local/bin/perl -w
use strict;
# Program:
use App::Daemon qw( detach );
detach();
sleep(5);
open FILE, ">foo.txt" or die;
print FILE "Waaah!\n";
close FILE;
use Test::More tests => 4;
use App::Daemon qw(daemonize cmd_line_parse);
use File::Temp qw(tempfile);
use Fcntl qw/:flock/;
my($fh, $tempfile) = tempfile();
my($pf, $pidfile) = tempfile();
# Turdix locks temp files, so unlock them just in case
flock $fh, LOCK_UN;
flock $pf, LOCK_UN;
ok(1, "loaded ok");
open(OLDERR, ">&STDERR");
open(STDERR, ">$tempfile");
@ARGV = ("-X", "-p", $pidfile);
daemonize();
close STDERR;
open(STDERR, ">&OLDERR");
close OLDERR;
ok(1, "running in foreground with -X");
open PIDFILE, "<$pidfile";
my $pid = <PIDFILE>;
chomp $pid;
close PIDFILE;
is($pid, $$, "check pid");
open FILE, "<$tempfile";
my $data = join '', <FILE>;
close FILE;
like($data, qr/Written to $pidfile/, "log message");
use Test::More tests => 4;
use App::Daemon qw(daemonize cmd_line_parse);
use File::Temp qw(tempfile);
use Fcntl qw/:flock/;
my($fh, $tempfile) = tempfile();
my($pf, $pidfile) = tempfile();
# Turdix locks temp files, so unlock them just in case
flock $fh, LOCK_UN;
flock $pf, LOCK_UN;
ok(1, "loaded ok");
open(OLDERR, ">&STDERR");
open(STDERR, ">$tempfile");
@ARGV = ();
$App::Daemon::background = 0;
$App::Daemon::pidfile = $pidfile;
daemonize();
close STDERR;
open(STDERR, ">&OLDERR");
close OLDERR;
ok(1, "running in foreground");
open PIDFILE, "<$pidfile";
my $pid = <PIDFILE>;
chomp $pid;
close PIDFILE;
is($pid, $$, "check pid");
open FILE, "<$tempfile";
my $data = join '', <FILE>;
close FILE;
like($data, qr/Written to $pidfile/, "log message");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment