<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mikhail panchenko / blog &#187; ubuntu</title>
	<atom:link href="http://mihasya.com/blog/category/ubuntu/feed/" rel="self" type="application/rss+xml" />
	<link>http://mihasya.com/blog</link>
	<description>good things now come in packages of three</description>
	<lastBuildDate>Mon, 02 Aug 2010 18:47:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>daemontools 0.76 on Ubuntu 9.04</title>
		<link>http://mihasya.com/blog/daemontools-0-76-on-ubuntu-9-04/</link>
		<comments>http://mihasya.com/blog/daemontools-0-76-on-ubuntu-9-04/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 02:25:16 +0000</pubDate>
		<dc:creator>mihasya</dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[daemontools]]></category>
		<category><![CDATA[supervise]]></category>

		<guid isPermaLink="false">http://mihasya.com/blog/?p=272</guid>
		<description><![CDATA[I find daemontools to be incredibly useful for quickly turning scripts into daemons. I also like to run the latest version of Ubuntu, which means there is no (well-maintained) package available. There&#8217;s a package in &#8216;universe&#8217; but it puts things in strange places and generally inspires distrust (explained below). As installing daemontools from source on [...]]]></description>
			<content:encoded><![CDATA[<p>I find <strong>daemontools</strong> to be incredibly useful for quickly turning scripts into daemons. I also like to run the latest version of Ubuntu, which means there is no (well-maintained) package available. There&#8217;s a package in &#8216;universe&#8217; but it puts things in strange places and generally inspires distrust (explained below). As installing daemontools from source on Ubuntu hasn&#8217;t been straight forward (I had trouble both on 8.10 and 9.04), I&#8217;m gonna post a quick howto which pulls together a few pieces of information scattered throughout the internets.</p>

<h3>Crash Course</h3>

<p>daemontools comes with a bunch of tools. The full docs are <a title="daemontools docs" href="http://cr.yp.to/daemontools.html">here</a>, but I&#8217;ll cover the basics.</p>

<ul>
    <li><strong>supervise</strong> is a daemon that takes a directory as an argument and tries to execute the file named &#8216;run&#8217; in that directory, while keeping state in the &#8216;supervise&#8217; subdirectory. Naturally, you want to give it a folder with an executable file named &#8216;run&#8217; and a wirtable subdirectory called &#8216;supervise&#8217; in it.</li>
    <li><strong>svstat</strong>, <strong>svok</strong>, <strong>svc</strong>, etc are used to manipulate individual supervise jobs and check their status. These aren&#8217;t covered here</li>
    <li><strong>svscan</strong> is a daemon that also takes a directory as an argument and proceeds to look for supervise-able directories within. Any time it finds one that doesn&#8217;t already have a corresponding supervise process, it fires one up. Thus if the supervise instance you count on to do some background processing crashes for a whatever reason, svscan will just start it back up</li>
    <li><strong>svscanboot</strong> is the script that should be run at boot-time to start svscan. It will direct svscan to look for jobs in the /service directory.</li>
</ul>

<p>In an ideal world, you throw a bunch of shit into /service, <strong>svscan</strong> picks it up and fires up a <strong>supervise</strong> process for all the subdirectories. It runs as a daemon and restarts supervise whenever needed (svscan itself is basically bulletproof in my experience). When your machine is rebooted for whatever reason, <strong>svscanboot</strong> restarts svscan and everything resumes as before. <em>The reason I choose not to use the Ubuntu package is because it does not create this ideal world for me</em>.</p>

<h3>Compiling</h3>

<p>First of all, daemontools won&#8217;t compile as is. Follow along the instructions <a title="daemontools installation instructions" href="http://cr.yp.to/daemontools/install.html">here</a> until you get to the exciting part where you type &#8216;package/install&#8217; and then get an error message that looks like this:</p>

<blockquote>
<pre>/usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss
mismatches non-TLS reference in envdir.o
/lib/libc.so.6: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [envdir] Error 1</pre>
</blockquote>

<p>Never fret. I have found a couple of possible fixes, but the patch below appears to be the cleanest one. [1]</p>

<blockquote>
<pre>diff -ur daemontools-0.76.old/src/error.h daemontools-0.76/src/error.h
--- daemontools-0.76.old/src/error.h    2001-07-12 11:49:49.000000000 -0500
+++ daemontools-0.76/src/error.h    2003-01-09 21:52:01.000000000 -0600
@@ -3,7 +3,7 @@
#ifndef ERROR_H
#define ERROR_H

-extern int errno;
+#include &lt;errno.h&gt;

extern int error_intr;
extern int error_nomem;</pre>
</blockquote>

<p>Assuming you are still at the point in the installation instructions right before the &#8216;package/install&#8217; command, copy the patch into /tmp/daemontools-errno.patch, then
<pre>$ cd src
$ patch &lt; /tmp/daemontools-errno.patch
$ cd ..</pre>
Then resume the original daemontools instructions as prescribed.</p>

<h3>Testing</h3>

<p>The installation creates two folders: <strong>/command</strong> and <strong>/service</strong>. The former contains all the different daemontools commands, while the latter is the default folder which the svscan daemon scans for jobs. To test that our installation worked, let&#8217;s create a proper test job (you&#8217;ll probably want to be superuser for this, since /service is owned by root by default)
<pre>$ cd /service
$ mkdir tester
$ mkdir tester/supervise
$ touch tester/run
$ chmod +x tester/run</pre>
in tester/run, put any script. It can be empty, but if you really want to verify things are working, make it output something to a tmp file. Sample:</p>

<blockquote>
<pre>#!/bin/bash

echo "Foo!" &gt;&gt; /tmp/foo.log
sleep 5</pre>
</blockquote>

<p>Once everything is in place, as superuser run the following commands. If your output looks different, something&#8217;s broke.
<pre>$ /command/svscan /service &amp;
[1] 9641
$ ps -ef | grep svscan
root      9641 17108  0 18:12 pts/1    00:00:00 /command/svscan /service
mikhailp  9646 17108  0 18:12 pts/1    00:00:00 grep svscan
$ ps -ef | grep supervise
root      9642  9641  0 18:12 pts/1    00:00:00 supervise tester
mikhailp  9660 17108  0 18:12 pts/1    00:00:00 grep supervise</pre>
If you made your tester script log something, check that the output is showing up where expected. If all is well, shut &#8216;er down and move on.
<pre>$ killall svscan
$ killall supervise</pre></p>

<h3>Starting at Bootup</h3>

<p>Assuming you want to user supervise for some critical offline part of your application, you want it to start automatically whenever your server boots up. You don&#8217;t want to have to manually start up svscan every time.</p>

<p>Ubuntu uses <a title="upstart docs" href="http://upstart.ubuntu.com/getting-started.html">upstart</a> for task management instead of sysvinit. In order to let upstart start and stop the svscan daemon correctly, the following script needs to be copied into /etc/event.d/svscan [2]</p>

<blockquote>
<pre># svscan - daemontools
#
# This service starts daemontools from the point the system is
# started until it is shut down again.

start on startup

start on runlevel 1
start on runlevel 2
start on runlevel 3
start on runlevel 4
start on runlevel 5
start on runlevel 6

stop on shutdown

exec /command/svscanboot</pre>
</blockquote>

<p>Once that&#8217;s in place, you can use <strong>start svscan</strong> and <strong>stop svscan</strong>. Try starting it and checking ps output as described in the Testing section. Finally, reboot the box (if possible) and repeat the same checks. At this point, svscan should start up with your system and spawn supervise processes on any job it finds in /service.</p>

<p>That&#8217;s all there is to it. Enjoy.</p>

<p>1: Patch originally found <a title="daemontools errno patch" href="http://www.qmailrocks.org/downloads/patches/daemontools-0.76.errno.patch">here</a>, linked from <a title="daemontools fedora howto" href="http://directory.fedoraproject.org/wiki/Howto:Daemontools">here</a>.
2: Corrected from <a title="daemontools upstart blogpost" href="http://blog.blinkenlights.nl/2006/10/29/daemontools-with-upstart/">this entry</a> posted in 2006 which has some obsolete info</p>
]]></content:encoded>
			<wfw:commentRss>http://mihasya.com/blog/daemontools-0-76-on-ubuntu-9-04/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
