hdaemonize: Library to handle the details of writing daemons for UNIX

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

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).

[Skip to Readme]


Versions 0.1, 0.2, 0.3, 0.4, 0.4.1, 0.4.2, 0.4.3, 0.4.4,,,,,, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.5.6, 0.5.6, 0.5.7
Change log None available
Dependencies base (>=4 && <5), bytestring, extensible-exceptions, filepath, hsyslog (>=5 && <6), mtl, unix [details]
License BSD-3-Clause
Author Anton Tayanovskyy, Fred Ross, Lana Black
Maintainer Jeremy Bornstein <jeremy@jeremy.org>
Category System
Home page http://github.com/unprolix/hdaemonize
Source repo head: git clone https://github.com/unprolix/hdaemonize.git
Uploaded by jeremy at 2019-11-11T13:35:21Z


[Index] [Quick Jump]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Readme for hdaemonize-0.5.6

[back to package description]


Build Status

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


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


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.



Jeremy Bornstein jeremy@jeremy.org

Lana Black lanablack@amok.cc

Anton Tayanovskyy name.surname@gmail.com.

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