Daemon::Generic(3)
π NAME π
Daemon::Generic(3) β User Contributed Perl Documentation β and the actual module name is Daemon::Generic, a framework to provide start/stop/reload for a daemon. Slay. π
β¨ SYNOPSIS β¨
okay so the whole setup is literally this:
use Daemon::Generic;
sub gd_run { ... stuff }
newdaemon();
π¦ DESCRIPTION π¦
Daemon::Generic gives you a framework for starting, stopping, and reconfiguring daemon-like programs. It provides standard commands that work for 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. No cap. β¨
πΈ EXAMPLE USAGE OUTPUT πΈ
Unless overridden, 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
π CONSTRUCTION π
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).
progname($0) β the name of this program. This will be used for logging and for naming the PID file.configfile("/etc/$progname.conf") β the location of the configuration file for this daemon.pidbase(/var/run/$progname) β we include the configuration file name as part of the pid file in case there are multiple instances of this daemon. The pidbase is the part of the PID file that does not include the configuration file name.pidfile("$pidbase.$configfile.pid") β the location of the process id file.foreground(0) β do not detach/daemon and run in the foreground instead.debug(0) β turn on debugging.no_srand(0) β normallysrand()is called. Ifno_srandis set thensrand()won't be called.options(()) β additional arguments forGetopt::Long::GetOptionswhich is used to parse@ARGV. Alternatively: define&gd_more_opt().minimum_args(1) β minimum number of@ARGVarguments after flags have been processed.maximum_args(1) β maximum number of@ARGVarguments after flags have been processed.version($pkg::VERSION) β the version number of the daemon.logpriorityβ used forlogger -p.
π¨ MUST-OVERRIDE CALLBACK METHODS π¨
The package that subclasses Daemon::Generic must provide the following callback methods.
gd_run()β this is where you put your main program. It is okay to change userid/group as the first action ofgd_run().
π MAY-OVERRIDE CALLBACK METHODS π
The package that subclasses Daemon::Generic does not have to override these methods but it may want to. Bestie, this is the optional menu β take what you need. π
π
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").
Most uses of Daemon::Generic should define gd_preconfig.
π gd_postconfig(%config)
Called only when the daemon is actually starting up. (Or on reconfigs.) It is passed the return value from gd_preconfig.
π§ gd_setup_signals()
Set things up so that SIGHUP calls 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()
This is invoked to parse the command line. Useful things to modify are:
$self->{configfile}β the location of the configuration file to be parsed bygd_preconfig().$self->{foreground}β run in the foreground (don't daemonize).$self->{debug}β use it yourself.
The supplied gd_getopt() method uses Getopt::Long.
π gd_parse_argv()
Parse any additional command line arguments beyond what gd_getopt() handled.
$ARGV[0] needs to be left alone if it is one of the following standard items:
startβ start up a new daemon.stopβ stop the running daemon.restartβ stop the running daemon, start a new one.reloadβ send a signal to the running daemon, asking it to reconfigure itself.checkβ just check the configuration file.helpβ print the help screen (probablyusage()).versionβ display the daemon's version.
There is no default gd_parse_argv().
π¦ gd_check($pidfile, $pid)
Normal behavior: return. Define additional checks to run when the check command is given. A $pid will only be supplied if there is a daemon running.
β gd_version()
Normal behavior: display a version message and exit.
π gd_help()
Normal behavior: call gd_usage().
π gd_commands_more()
Used by 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_flags_more
Like gd_commands_more() but defines additional command line flags. There should also be a gd_more_opt() or an "options" argument to new().
π gd_positional_more
Like gd_commands_more() but defines positional arguments.
π
gd_usage()
Display a usage message. The return value from gd_usage() is the exit code for the program.
π gd_more_opt()
() Additional arguments for Getopt::Long::GetOptions which is used to parse @ARGV. Alternatively: pass "options" to new().
π§΅ gd_pidfile()
Figure out where the PID file should be.
π¨ gd_error()
Print out an error (call die?).
π― gd_other_cmd()
Called when $ARGV[0] isn't one of the commands that Daemon::Generic knows by default. Default behavior: call gd_usage() and exit(1).
π gd_daemonize()
Normal behavior: fork(), fork(), detach from tty.
π gd_redirect_output()
This is a mis-named method. Sorry. This directs STDOUT/STDERR/STDIN to /dev/null as part of daemonizing. Used by gd_daemonize().
π gd_reopen_output()
After daemonizing, output file descriptors are re-established. Normal behavior: redirect STDOUT and STDERR to logger -t $progname[$$]. Used by gd_daemonize().
π§ gd_logname()
Normal behavior: $progname[$$]. Used by gd_redirect_output().
π₯ gd_reconfig_event()
Normal behavior: call gd_postconfig(gd_preconfig)). Only referenced by gd_setup_signals().
π gd_quit_event()
Normal behavior: exit. Only referenced by gd_setup_signals().
π gd_kill_groups()
Return true if gd_kill should kill process groups ($pid) instead of just the one daemon ($pid). Default is false.
π₯ gd_kill($pid)
Used by the 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).
π οΈ gd_install
Installs the daemon so that it runs automatically at next reboot. Currently done with a symlink to $0 and /usr/sbin/update-rc.d. Please send patches for other methods!
β
gd_can_install
Returns a function to do an gd_install if installation is possible. Returns 0 otherwise.
π¬ gd_install_pre($method)
Normal behavior: return. Called just before doing an installation. The method indicates the installation method (currently always "update-rc.d".)
π gd_install_post($method)
Normal behavior: return. Called just after doing an installation.
π§Ή gd_uninstall
Will remove the daemon from the automatic startup regime.
π§Ό gd_can_uninstall
Returns a function to do the work for gd_uninstall if it's possible. 0 otherwise.
πͺ gd_uninstall_pre($method)
Normal behavior: return. Called just before doing an un-installation. The method indicates the installation method (currently always "update-rc.d".)
π gd_install_post($method)
Normal behavior: return. Called just after doing an un-installation.
πΎ MEMBER DATA πΎ
Okay bestie, since you're subclassing Daemon::Generic, you lowkey need to know what its internal data structures look like. With exactly two exceptions, every member data item starts with the prefix gd_ β that's the vibe check for this section. π
configfileβ where the configuration file lives. (Not used byDaemon::Generic.)debugβ are we showing debugging? (Not used byDaemon::Generic.)gd_argsβ the original%argsthat got passed tonew.gd_prognameβ the process name. (Defaults to$0.)gd_pidfileβ the location of the process ID file.gd_logpriorityβ used forlogger -p.gd_foregroundβ are we running in the foreground?
π EXAMPLE PROGRAM π
Here's the whole little example, and yes, it's giving main-character daemon energy. πΈ
my $sleeptime = 1;
newdaemon(
progname => 'ticktockd',
pidfile => '/var/run/ticktockd.pid',
configfile => '/etc/ticktockd.conf',
);
sub gd_preconfig
{
my ($self) = @_;
open(CONFIG, "<$self->{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";
}
}
β¨ SEE ALSO β¨
- With a
while(1)and delayed signal delivery:Daemon::Generic::While1. - With
Event:Daemon::Generic::Event. - With
AnyEvent:Daemon::Generic::AnyEvent. - Modules that use
Daemon::Generic:SyslogScan::Daemon;IO::Event(rinetd.pl) - Other modules that do similar things:
Net::Daemon,Net::Server,Net::Server::Daemonize,NetServer::Generic,Proc::Application::Daemon,Proc::Daemon,Proc::Forking.
π LICENSE π
Legal tea time, bestie β: Copyright (C) 2006-2010 David Muir Sharnoff <cpan@dave.sharnoff.org>. and Copyright (C) 2011 Google, Inc. This module may be used and distributed on the same terms as Perl itself.
π¦ PACKAGERS π¦
Daemon::Generic is packaged for Fedora by Emmanuel Seyman <emmanuel.seyman@club-internet.fr>.
That's the whole page, bestie π«Ά β source/version perl v5.42.0, dated 2017-07-17, from Daemon::Generic(3). Byeeeee π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Daemon-Generic 0.85-bp160.1.11 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms