Add an option to daemon(8) to specify a delay between restarts of a

supervised program.  The existing -r option has a hard-coded delay of one
second.  This change adds a -R option which takes a delay in seconds.  This
can be used to prevent log spam and rapid restarts, similar to init(8)'s
behavior of adding a delay between rapid restarts when it's supervising a
program.
This commit is contained in:
Ian Lepore 2018-04-15 21:46:08 +00:00
parent 0d7404ba76
commit 37820b8746
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=332518
2 changed files with 15 additions and 5 deletions

View File

@ -26,7 +26,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd October 22, 2016
.Dd April 14, 2018
.Dt DAEMON 8
.Os
.Sh NAME
@ -44,6 +44,7 @@
.Op Fl s Ar syslog_priority
.Op Fl T Ar syslog_tag
.Op Fl l Ar syslog_facility
.Op Fl T Ar restart_delay_seconds
.Ar command arguments ...
.Sh DESCRIPTION
The
@ -114,7 +115,11 @@ regardless of whether the
.Fl u
option is used or not.
.It Fl r
Supervise and restart the program if it has been terminated.
Supervise and restart the program after a one-second delay if it has
been terminated.
.It Fl R restart_delay_seconds
Supervise and restart the program after the specified delay
if it has been terminated.
.It Fl t Ar title
Set the title for the daemon process.
The default is the daemonized invocation.

View File

@ -99,7 +99,7 @@ main(int argc, char *argv[])
dosyslog = 0;
outfn = NULL;
title = NULL;
while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:T:")) != -1) {
while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:R:T:")) != -1) {
switch (ch) {
case 'c':
nochdir = 0;
@ -130,6 +130,11 @@ main(int argc, char *argv[])
case 'r':
restart = 1;
break;
case 'R':
restart = strtol(optarg, &p, 0);
if (p == optarg || restart < 1)
errx(6, "invalid restart delay");
break;
case 's':
logpri = get_log_mapping(optarg, prioritynames);
if (logpri == -1)
@ -359,7 +364,7 @@ main(int argc, char *argv[])
goto exit;
}
if (restart && !terminate) {
daemon_sleep(1, 0);
daemon_sleep(restart, 0);
close(pfd[0]);
pfd[0] = -1;
goto restart;
@ -558,7 +563,7 @@ usage(void)
"usage: daemon [-cfrS] [-p child_pidfile] [-P supervisor_pidfile]\n"
" [-u user] [-o output_file] [-t title]\n"
" [-l syslog_facility] [-s syslog_priority]\n"
" [-T syslog_tag] [-m output_mask]\n"
" [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n"
"command arguments ...\n");
exit(1);
}