random technical thoughts from the Nominet technical team

OSX Launchd and NSD

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4 out of 5)
Loading ... Loading ...
Posted by roy on Oct 16th, 2007

I’ve installed NSD on Apple’s OSX. Since rc functionality will be deprecated in the near future, I decided to have a go with launchd. Launchd offers a single method to any programs started automatically by the system. You can do a lot more, like specifying resource limits and environment variables, but I’ll leave that out of this post.

Opposed to writing scripts and reference them through rc.local, I have to write an xml plist file. This file should be put in the /Library/LaunchDaemons directory. There is also a /System/Library/LaunchDaemons directory, though that is reserved for system provided daemons, and hence may have changed after an update. Have a look though at some of the plist files in those directories to understand how things are done.

Let’s construct a plist file for NSD. Since the plist files are in XML, we’ll start of by referring to a document type definition (dtd). We need to include the property list version (1.0) as well.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

Now we’re ready to fill out the property list keys. We’ll use “nl.nlnetlabs.nsd” as a label. Though this is required, it could really be anything, as long as its unique. Since the labels of other daemons seem to look like a name space definition, let’s mimic that.

        <key>Label</key>
        <string>nl.nlnetlabs.nsd</string>

One thing to note is that Launchd takes care of forking and daemonizing, so we’ll need to configure NSD in such a way that itself does not daemonize. (We can’t get it to stop forking). This can be done by using the “-d” flag, which causes NSD to run in debug mode, and not daemonize. Take care of the path if your NSD resides elsewhere than /usr/local/sbin . You could also configure that in your nsd.conf.

        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/sbin/nsd</string>
                <string>-d</string>
        </array>

We need to instruct Launchd that NSD is run at load time.

        <key>RunAtLoad</key>
        <true/>

We also need to instruct Launchd that NSD must be kept running continuously, i.e. restarted immediately after a crash.

        <key>OnDemand</key>
        <false/>

Finally, we need to add the closing tags:

</dict>
</plist>

The entire file should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>nl.nlnetlabs.nsd</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/sbin/nsd</string>
                <string>-d</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>OnDemand</key>
        <false/>
</dict>
</plist>

After you’ve copied the above in the file /Library/LaunchDaemons/nl.nlnetlabs.nsd.plist you can now start the daemon. You can do it by using launchctl, as follows:

$ sudo launchctl load -w /Library/LaunchDaemon/nl.nlnetlabs.nsd.plist

There is a very handy GUI editor for these kind of launchd plist files by Peter Borg called Lingon. This allows you fine grained configuration over your launchd plist files, and acts as an interface to control launchd.

One Response

  1. Carsten Strotmann Says:

    Thanks for this useful post.

    I have tried this reciepe for “unbound” and found that the directory for LaunchDaemon XML configs is in /Library/LaunchDaemons/ instead of /Library/LaunchDaemon/ (there is an ’s’ missing)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Recent Posts

Highest Rated

Categories

Archives

Meta: