From 51efb21713ee630ca9c2f6aaaea98f18bce72106 Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Wed, 19 Jul 2017 21:45:00 +1000 Subject: daemon: Add ability to write a PID file The PID file will only be written if the daemon is called with the `--detach` command line argument and a `--pidfile /some/file/path` argument. --- src/daemonizer/posix_daemonizer.inl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/daemonizer/posix_daemonizer.inl') diff --git a/src/daemonizer/posix_daemonizer.inl b/src/daemonizer/posix_daemonizer.inl index f8be15dda..506c7766f 100644 --- a/src/daemonizer/posix_daemonizer.inl +++ b/src/daemonizer/posix_daemonizer.inl @@ -43,6 +43,10 @@ namespace daemonizer "detach" , "Run as daemon" }; + const command_line::arg_descriptor arg_pidfile = { + "pidfile" + , "File path to write the daemon's PID to (optional, requires --detach)" + }; const command_line::arg_descriptor arg_non_interactive = { "non-interactive" , "Run non-interactive" @@ -55,6 +59,7 @@ namespace daemonizer ) { command_line::add_arg(normal_options, arg_detach); + command_line::add_arg(normal_options, arg_pidfile); command_line::add_arg(normal_options, arg_non_interactive); } @@ -80,7 +85,12 @@ namespace daemonizer if (command_line::has_arg(vm, arg_detach)) { tools::success_msg_writer() << "Forking to background..."; - posix::fork(); + std::string pidfile; + if (command_line::has_arg(vm, arg_pidfile)) + { + pidfile = command_line::get_arg(vm, arg_pidfile); + } + posix::fork(pidfile); auto daemon = executor.create_daemon(vm); return daemon.run(); } -- cgit v1.2.3