Upstart and unison

Well, that was ridiculously difficult. Hope this helps you avoid the pain.

upstart is very useful, and nicely free of the complexities of the old SysVInit system, but the author forgot a few things:

  1. Examples
  2. Easy debugging

When upstart first came out, it had essentially no examples, and no documentation. To successfully use it, you either had to: be the author, or: read the entire source code. Not ideal. He has since wised up, and there is now Upstart Intro, Cook Book and Best Practices.

So when I need to add unison as a daemon on my system, I chose the "easy" way - upstart. Not quite as easy, because of the second missing thing - easy debugging. upstart comes with a distinct lack of easy logging (you have to read through the cookbook to find examples), nor does the author see the need to help (I had filed a bug with him, which languished for months before he closed it without any useful help).

So, the problem I had was that upstart would try to start unison, and fail, with a cryptic error code, and I could not log any info. unison requires the kernel's options to be modified, and then it only logs its own actions - not any output from the actual program.

After using a technique provided in Logging a Daemon's Output with Upstart - Server Fault, I finally discovered that unison's logging wasn't working because it never got far enough - it failed with this:

Fatal error: exception Util.Fatal("Environment variable HOME not found")

So, once I discovered that little tidbit, I modified the upstart script, and here it is:

description "unison synchronization service"
author      "aji <aji@rogan>"

start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [!2345]

respawn

script
    # unison requires $HOME
    export HOME=/root
    # start unison as server on socket 12345, logging all errors to /tmp/...
    /usr/bin/unison -socket 12345 >> /tmp/upstart_unison-socket.log 2>&1
end script

Remember to use an available port. Enjoy.


Written by Andrew Ittner in public on Sat 26 March 2011. Tags: linux