added a new cli option --daemonize

This option forks the process and generates a new session. This
effectively detaches it from the shell. Don't forget to redirect stderr
or use syslog for logging!
This commit is contained in:
Christian Kruse
2014-01-08 11:53:15 +01:00
parent 9fe2d6886e
commit 920f925e4b
2 changed files with 32 additions and 1 deletions

View File

@@ -35,5 +35,6 @@
#define ERR_STR_OVERFLOW 10 #define ERR_STR_OVERFLOW 10
#define ERR_FAILOVER_FAIL 11 #define ERR_FAILOVER_FAIL 11
#define ERR_BAD_SSH 12 #define ERR_BAD_SSH 12
#define ERR_SYS_FAILURE 13
#endif /* _ERRCODE_H_ */ #endif /* _ERRCODE_H_ */

View File

@@ -150,11 +150,13 @@ main(int argc, char **argv)
{"config", required_argument, NULL, 'f'}, {"config", required_argument, NULL, 'f'},
{"verbose", no_argument, NULL, 'v'}, {"verbose", no_argument, NULL, 'v'},
{"monitoring-history", no_argument, NULL, 'm'}, {"monitoring-history", no_argument, NULL, 'm'},
{"daemonize", no_argument, NULL, 'd'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
int optindex; int optindex;
int c; int c;
bool daemonize = false;
char standby_version[MAXVERSIONSTR]; char standby_version[MAXVERSIONSTR];
@@ -174,7 +176,7 @@ main(int argc, char **argv)
} }
} }
while ((c = getopt_long(argc, argv, "f:v:m", long_options, &optindex)) != -1) while ((c = getopt_long(argc, argv, "f:v:md", long_options, &optindex)) != -1)
{ {
switch (c) switch (c)
{ {
@@ -187,12 +189,39 @@ main(int argc, char **argv)
case 'm': case 'm':
monitoring_history = true; monitoring_history = true;
break; break;
case 'd':
daemonize = true;
break;
default: default:
usage(); usage();
exit(ERR_BAD_CONFIG); exit(ERR_BAD_CONFIG);
} }
} }
if (daemonize)
{
pid_t pid = fork();
switch (pid)
{
case -1:
log_err("Error in fork(): %s\n", strerror(errno));
exit(ERR_SYS_FAILURE);
break;
case 0: // child process
pid = setsid();
if (pid == (pid_t)-1)
{
log_err("Error in setsid(): %s\n", strerror(errno));
exit(ERR_SYS_FAILURE);
}
break;
default: // parent process
exit(0);
}
}
setup_event_handlers(); setup_event_handlers();
/* /*
@@ -1116,6 +1145,7 @@ void help(const char *progname)
printf(_(" --verbose output verbose activity information\n")); printf(_(" --verbose output verbose activity information\n"));
printf(_(" --monitoring-history track advance or lag of the replication in every standby in repl_monitor\n")); printf(_(" --monitoring-history track advance or lag of the replication in every standby in repl_monitor\n"));
printf(_(" -f, --config_file=PATH configuration file\n")); printf(_(" -f, --config_file=PATH configuration file\n"));
printf(_(" -d, --daemonize detach process from foreground\n"));
printf(_("\n%s monitors a cluster of servers.\n"), progname); printf(_("\n%s monitors a cluster of servers.\n"), progname);
} }