hdaemonize-0.2: utilities for UNIX daemon writing




type Logger = String -> IO ()Source

The simplest possible interface to syslog.

type Program = Logger -> IO ()Source

A program is any IO computation. It also accepts a syslog handle.

daemonize :: IO () -> IO ()Source

Daemonizes a given IO computation by forking twice, closing standard file descriptors, blocking sigHUP, setting file creation mask, starting a new session, and changing the working directory to root.

serviced :: Program -> IO ()Source

Turns a program into a UNIX daemon (system service) ready to be deployed to etcrc.d or similar startup folder. The resulting program handles command-line arguments (start, stop, or restart).

With start option it writes out a PID to varrun/$name.pid where $name is the executable name. If PID already exists, it refuses to start, guaranteeing there is only one live instance.

With stop option it reads the PID from varrun/$name.pid and terminates the corresponding process (first a soft kill, SIGTERM, then a hard kill, SIGKILL).

Another addition over the daemonize function is dropping privileges. If a system user and group with a name that matches the executable name exist, privileges are dropped to that user and group. Otherwise, they are dropped to the standard daemon user and group.

Finally, exceptions in the program are caught, logged to syslog, and the program restarted.