-z [ --no-config ] start without a configuration file
-C [ --validate ] exit after validating the configuration
-e [ --errorlog ] arg log fatal errors to the specified log file (only
- works in combination with --daemonize)
+ works in combination with --daemonize or
+ --close-stdio)
-d [ --daemonize ] detach from the controlling terminal
+ --close-stdio do not log to stdout (or stderr) after startup
Report bugs at <https://github.com/Icinga/icinga2>
Icinga home page: <https://www.icinga.com/>
Log(LogDebug, "Daemonize()")
<< "Child process with PID " << Utility::GetPid() << " continues; re-initializing base.";
+ // Detach from controlling terminal
+ pid_t sid = setsid();
+ if (sid == -1) {
+ Log(LogCritical, "cli")
+ << "setsid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
+ exit(EXIT_FAILURE);
+ }
+
try {
Application::InitializeBase();
} catch (const std::exception& ex) {
#endif /* _WIN32 */
}
-static bool SetDaemonIO(const String& stderrFile)
+static void CloseStdIO(const String& stderrFile)
{
#ifndef _WIN32
int fdnull = open("/dev/null", O_RDWR);
if (fderr > 2)
close(fderr);
}
-
- pid_t sid = setsid();
- if (sid == -1) {
- return false;
- }
#endif
-
- return true;
}
String DaemonCommand::GetDescription() const
("config,c", po::value<std::vector<std::string> >(), "parse a configuration file")
("no-config,z", "start without a configuration file")
("validate,C", "exit after validating the configuration")
- ("errorlog,e", po::value<std::string>(), "log fatal errors to the specified log file (only works in combination with --daemonize)")
+ ("errorlog,e", po::value<std::string>(), "log fatal errors to the specified log file (only works in combination with --daemonize or --close-stdio)")
#ifndef _WIN32
("daemonize,d", "detach from the controlling terminal")
+ ("close-stdio", "do not log to stdout (or stderr) after startup")
#endif /* _WIN32 */
;
}
}
- if (vm.count("daemonize")) {
+ if (vm.count("daemonize") || vm.count("close-stdio")) {
+ // After disabling the console log, any further errors will go to the configured log only.
+ // Let's try to make this clear and say good bye.
+ Log(LogInformation, "cli", "Closing console log.");
+
String errorLog;
if (vm.count("errorlog"))
errorLog = vm["errorlog"].as<std::string>();
- SetDaemonIO(errorLog);
+ CloseStdIO(errorLog);
Logger::DisableConsoleLog();
}