Finally I've finished library for wrapping applications into daemons or services (Windows). The library hides platform-specific boilerplate behind compile-time API:
// First you need to describe your daemon via template
alias daemon = Daemon!(
    "DaemonizeExample1", // unique name

    // Setting associative map signal -> callbacks
// You can bind same delegate for several signals by Composition template // delegate can take additional argument to know which signal is caught Composition!(Signal.Terminate, Signal.Quit, Signal.Shutdown, Signal.Stop), (logger, signal)
            return false; // returning false will terminate daemon
        Signal.HangUp, (logger)
            logger.logInfo("Hello World!");
            return true; // continue execution

    // Main function where your code is
    (logger, shouldExit) {
        // will stop the daemon in 5 minutes
auto time = Clock.currSystemTick + cast(TickDuration)5.dur!"minutes";
        while(!shouldExit() && time > Clock.currSystemTick) {  }

        return 0;

int main()
return buildDaemon! shared StrictLogger("logfile.log"));

At the moment daemonize has following features:
* Daemons for GNU/Linux, services for Windows
* Custom signals
* Signal composition
* Client for sending signals to defined daemons
* Auto installing and uninstalling for Windows services
* Usage of .pid and .lock files (GNU/Linux)
* Privileges lowing (GNU/Linux)

Daemonize operates well with vibe.d (example -

P.S. At the moment library doesn't support Mac and other Posix systems, the support is going to be added at next releases.

Reply via email to