[Haskell-cafe] How to daemonize a threaded Haskell program?

David Anderson dave at natulte.net
Mon Mar 7 10:01:45 CET 2011

On Mon, Mar 7, 2011 at 12:39 AM, Bas van Dijk <v.dijk.bas at gmail.com> wrote:

> Sterling, Gregory, Brandon and David thanks for your suggestions.
> On 6 March 2011 05:38, David Anderson <dave at natulte.net> wrote:
> > I humbly recommend doing such daemonizations from outside your program.
> > Programs that daemonize on startup make it very difficult to monitor them
> by
> > direct means, instead forcing you to rely on PID files and other
> mechanisms
> > which may not always be available or fresh.
> Agreed, I already noticed that debugging is a bit harder since I have
> no stdout and stderr anymore.
> > For reference, Upstart, the new PID 1 on Ubuntu and friends, has a
> horrible
> > hack[1] built in specifically to keep track of processes that "helpfully"
> > daemonize themselves, so that it can offer additional services like
> > restarting crashed services or notifying the owner of a problem.
> > As has been pointed out elsewhere on thread, there are plenty of
> standalone
> > programs that implement daemonization if you end up not using service
> > management software to do it for you.
> I plan to run the daemon on a Ubuntu server. Do you know if Upstart is
> able to daemonize a process?

It's perfectly simple. In fact, it's the default assumption of Upstart that
your program doesn't daemonize, and that Upstart should help it. You have to
specify if your program violates that assumption.

Here's a sample file. It specifies a service that; should run whenever the
main runlevels are entered (i.e. basic system init like mounting filesystems
is done, but no other guarantee); stops when the system leaves those
runlevels; runs a binary that does not fork or daemonize; respawns the
service if the process should die for any reason; can be manually stopped
and restarted by the admin if he feels like it.

$ cat /etc/init/demo.conf
description "Demo service that doesn't daemonize by itself"
start on runlevel [2345]
stop on runlevel [!2345]
exec /path/to/binary
$ sudo start demo
demo start/running, process 27469
$ sudo stop demo
demo stop/waiting
$ sudo status demo
demo stop/waiting

Hope this helps. The upstart configuration format is a little sparsely
documented, when I wrote scripts for it, I used the existing files in
/etc/init as a rough guide.

- Dave

> Regards,
> Bas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110307/a7f91f0b/attachment.htm>

More information about the Haskell-Cafe mailing list