|
Daemon::Generic - framework to provide start/stop/reload for a daemon |
Daemon::Generic - framework to provide start/stop/reload for a daemon
use Daemon::Generic;
sub gd_run { ... stuff }
sub gd_preconfig { ... stuff }
newdaemon();
Daemon::Generic provides a framework for starting, stopping, reconfiguring daemon-like programs. The framework provides for standard commands that work for as init.d files and as apachectl-like commands.
Programs that use Daemon::Generic subclass Daemon::Generic to override its behavior. Almost everything that Genric::Daemon does can be overridden as needed.
Unless overriden, the usage output for your program will look something like this:
Usage: $progname [ -c file ] [ -f ] { start | stop | reload | restart | help | version | check }
-c Specify configuration file (defaults to $configfile)
-f Run in the foreground (don't detach)
start Starts a new $progname if there isn't one running already
stop Stops a running $progname
reload Causes a running $progname to reload it's config file. Starts
a new one if none is running.
restart Stops a running $progname if one is running. Starts a new one.
check Check the configuration file and report the daemon state
help Display this usage info
version Display the version of $progname
To hand control over to Daemon::Generic, call newdaemon().
Control will be handed back through method calls to functions you
define.
Your @ISA will be modified to include Daemon::Generic if
if it isn't already there.
These are the arguments to newdaemon().
Defaults are in (parenthesis).
$0) The name of this program. This will be used for
logging and for naming the PID file.
/etc/$progname.conf) The location of the configuration
file for this daemon.
$pidbase.$configfile.pid) The location of the
process id file.
0) Do not detach/daemon and run in the foreground instead.
0) Turn on debugging.
0) Normall srand() is called. If no_srand is set then
srand() won't be called.
@ARGV. Alternatively: define &gd_more_opt().
1) Minimum number of @ARGV arguments after flags have been
processed.
1) Maximum number of @ARGV arguments after flags have been
processed.
$pkg::VERSION) The version number of the daemon.
logger -p.
The package that subclasses Daemon::Generic must provide the following callback methods.
gd_preconfig()gd_preconfig() is called to parse the configuration file
($self->{configfile}). Preconfig is called on all invocations
of the daemon (daemon reload, daemon check, daemon stop, etc).
It shouldn't start anything but it can and should verify that
the config file is fine.
The return value should be a hash. With one exception, the return
value is only used by gd_postconfig(). The exception is that
gd_preconfig() may return a revised PID file location (key pidfile).
gd_run()
The package that subclasses Daemon::Generic does not have to override these methods but it may want to.
gd_postconfig(%config)Postconfig() is called only when the daeamon is actually starting up.
(Or on reconfigs). It is passed the return value from gd_preconfig.
gd_setup_signals()gd_reconfig_event() and
SIGINT calls gd_quit_event(). It will call these at any time
so if you want to delay signal delivery or something you should
override this method.
gd_getopt()gd_preconfig().
The supplied gd_getopt() method uses the Getopt::Long manpage.
gd_parse_argv()gd_getopt()
handled.
$ARGV[0] needs to be left alone if it is one of the following
standard items:
There is no default gd_parse_argv().
check command is given. A $pid will only be supplied
if there is a daemon running.
gd_version()gd_help()gd_usage().
gd_commands_more()gd_usage(): provide information on additional commands
beyond start, stop, reload, etc. Return is an array of
key value pairs.
sub gd_commands_more
{
return (
savestate => 'Tell xyz server to save its state',
reset => 'Tell xyz servr to reset',
);
}
gd_commands_more() but defines additional command line flags.
There should also be a gd_more_opt() or an options argument to
new().
gd_commands_more() but defines positional arguments.
gd_usage()gd_usage() is the exit code for the program.
gd_more_opt()@ARGV. Alternatively: pass options to new().
gd_pidfile()gd_error()gd_other_cmd()$ARGV[0] isn't one of the commands that Daemon::Generic knows
by default. Default behavior: call gd_usage() and exit(1).
gd_daemonize()fork(), fork(), detach from tty.
gd_redirect_output()STDOUT and STDERR to
logger -t $progname[$$]. Used by gd_daemonize().
gd_logname()$progname[$$]. Used by gd_redirect_output().
gd_reconfig_event()gd_postconfig(gd_preconfig)).
Only referenced by gd_setup_signals().
gd_quit_event()gd_setup_signals().
gd_kill($pid)stop and restart commands to get rid of
the old daemon.
Normal behavior: send a SIGINT.
Check to see if process $pid has died. If it has not, keep checking
and if it's still alive. After
$Daemon::Generic::force_quit_delay seconds,
send a SIGTERM. Keep checking. After another
$Daemon::Generic::force_quit_delay seconds,
send a SIGKILL (-9). Keep checking. After
$Daemon::Generic::force_quit_delay * 4 seconds or 60 seconds
(whichever is smaller), give up and exit(1).
$0 and
/usr/sbin/update-rc.d. Please send patches for other
methods!
gd_install if installation
is possible. Returns 0 otherwise.
gd_install_pre($method)update-rc.d.)
gd_install_post($method)gd_uninstall if it's
possible. 0 otherwise.
gd_uninstall_pre($method)update-rc.d.)
gd_install_post($method)
Since you need to subclass Daemon::Generic, you need to know
what the internal data structures for Daemon::Generic are.
With two exceptions, all of the member data items begin with
the prefix gd_.
%args passed to new.
logger -p.
my $sleeptime = 1;
newdaemon(
progname => 'ticktockd',
pidfile => '/var/run/ticktockd.pid',
configfile => '/etc/ticktockd.conf',
);
sub gd_preconfig
{
my ($self) = @_;
open(CONFIG, "<$self->{gd_configfile}") or die;
while(<CONFIG>) {
$sleeptime = $1 if /^sleeptime\s+(\d+)/;
}
close(CONFIG);
return ();
}
sub gd_run
{
while(1) {
sleep($sleeptime);
print scalar(localtime(time))."\n";
}
}
With a while(1) and delayed signal delivery: the Daemon::Generic::While1 manpage.
With the Event manpage: the Daemon::Generic::Event manpage.
Modules that use Daemon::Generic: the SyslogScan::Daemon manpage the IO::Event manpage (rinetd.pl)
Other modules that do similar things: the Net::Daemon manpage, the Net::Server manpage, the Net::Server::Daemonize manpage, the NetServer::Generic manpage, the Proc::Application::Daemon manpage, the Proc::Daemon manpage, the Proc::Forking manpage.
If you need high-speed internet services (T1, T3, OC3 etc), please send me your request-for-quote. I have access to very good pricing: you'll save money and get a great service.
Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com>.
This module may be used and distributed on the same terms
as Perl itself.
|
Daemon::Generic - framework to provide start/stop/reload for a daemon |