MyNixOS website logo
Description

Library to handle the details of writing daemons for UNIX.

Provides functions that help writing better UNIX daemons, daemonize and serviced/serviced': daemonize does what a daemon should do (forking and closing descriptors), while serviced does that and more (syslog interface, PID file writing, start-stop-restart command line handling, dropping privileges).

hdaemonize

Build Status

hdaemonize is a simple library that hides some of the complexities of writing UNIX daemons in Haskell.

Obtaining

The latest version is available (BSD license) at GitHub.

Using

The synopsis is:

import System.Posix.Daemonize
main = daemonize $ program

This code will make program do what good daemons should do, that is, detach from the terminal, close file descriptors, create a new process group, and so on.

If you want more functionality than that, it is available as a serviced function.

Here is an example:

import Control.Concurrent
import System.Posix.Daemonize

loop i log = do threadDelay $ 10^6
                log (show i)
        	    writeFile "/tmp/counter" $ show i
                if i == 5 then undefined else loop (i + 1) log

main = serviced (loop 0)

Let us say this program is compiled as mydaemon. Then:

# mydaemon start

starts the service. A second call to start will complain that the program is already running.

During its execution, mydaemon will simply write a new number to /tmp/counter every second, until it reaches 5. Then, an exception will be thrown. This exception will be caught by hdaemonize, and logged to /var/log/daemon.log or similar (this is depends on how syslog works on your platorm). log (show i) will leave messages in the same file.

When the exception is thrown, the program will be restared in 5 seconds, and will start counting from 0 again.

The following commands are also made available:

# mydaemon stop
# mydaemon restart

Finally, if configured to do so, mydaemon drops privileges by changing its effective user/group ID. (If no user/group ID are specified, it will continue execution as the original user and group.)

Note that if you wish to parse your own commandline arguments, you can replace invocation of the serviced function with serviced'. This requires specification of an Operation which indicates whether the daemon should be started, stopped, restarted, or whether its status should be queried.

Changelog

  • 0.5.6

    • Add serviced' function and Operation (Start, Stop, etc.) to allow invocation separated from commandline
    • Only attempt to change effective user/group ID when explicitly specified.
    • Do not attempt to set user or group to the daemon's name in the absence of a specified user or group.
  • 0.5.5

    • Fix a bug where hdaemonize fails when user or group "daemon" is absent
  • 0.5.4

    • Update to use hsyslog == 5.
  • 0.5.2

    • Fix pre-AMP builds.
  • 0.5.1

    • Updated to use hsyslog >=4
  • 0.4

    • added support for a privileged action before dropping privileges
  • 0.3

    • merged with updates by madhadron
  • 0.2

    • provided documentation
    • backported to older GHC versions, tested on 6.8.1
  • 0.1

    • initial public release

Authors

Jeremy Bornstein [email protected]

Lana Black [email protected]

Anton Tayanovskyy [email protected].

The code is originally based on a public posting by Andre Nathan, used by permission.

Metadata

Version

0.5.7

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows