Making a simple systemd file for Raspberry Pi jessie

[ UPDATE APRIL 4 2020 – there’s a correction to the file location (should be in /etc hierarchy… see comment thread at bottom). I’m leaving this posting as-is (as-was?) because I knew this worked at the time and no longer have a (now-old) jessie system to test. A quick google reveals this topic is well-covered in other sources these days too finally. Original article left below as it appeared ]

I wanted to write a systemd service file for a daemon I wanted started automatically at boot on my Raspberry Pi running Debian Jessie.

This used to be a lot simpler in the old hardcore Unix days. Well, no, actually, it’s simpler now. But figuring out how to do it was anything but simple and took me the better part of a day. Maybe I’m just bad at google, but I couldn’t find a simple “just do THIS” recipe; all I could find was hopelessly detailed and complex descriptions of systemd stuff that exceeded my newbie level of understanding and exceeded my geezer level of interest in reading lots of stuff.

So, here, in the hope that someone else will find this via google someday and find it useful, is how to do it. Or, at least, one way that works.

Step 1: Make a clown.service file

Assume you want /usr/local/bin/clown started at boot time, with the arguments: -name bozo

Make a file in /lib/systemd/system, called “clown.service”. Whatever you call this file (the “clown” part in this example) becomes the name of the service. Here’s what you need in the file:

Description=Clown server for jessie

ExecStart=/usr/local/bin/clown -name bozo


The key things you may need to change (besides the obvious command name and arguments) are the Type and the Restart entries.

Type=simple says your program doesn’t fork (i.e., needs to be “daemonized” by systemd). There are other options; look these up in the systemd documentation yourself if you have a more complex scenario.

Restart=always says that any time the program exits it should be started right back up again. This is exactly what I needed as my program isn’t supposed to die, but if it does I want it started again. The systemd program implements throttles on how often this happens in case things have gone haywire.

The [Install] section tells it to run the service whenever the system is in multi-user mode, and the After= part up top tells it not to start the service until the network is up.

Step 2: Tell systemd about this file

It’s not enough to just drop this file into the /lib/systemd/system directory. You also need to execute this command (just once, ever, for a new service file):

% sudo systemctl enable clown.service

You need to do that as root (hence the sudo, or just be root). You’ll get a message about it creating some symbolic links, which is good.

And … that’s it. ¬†Reboot and pray! Try looking in /var/log/daemon.log if you encounter any problems.

9 Replies to “Making a simple systemd file for Raspberry Pi jessie”

  1. I think you’ve got the path slightly wrong.

    You put “Make a file in /lib/system/systemd”

    Should that be /lib/systemd/system ?

  2. Was installing shairport-sync on my r-pi. Used a bit of your article (specifically the location of service files) to debug the shairport-sync service. You may want to mention in your article that service errors are logged to /var/log/daemon.log.

  3. Hello Neil I tried using this method to get raspivid to pipe a stream to my desktop and just having no success.

    I can get raspivid to start and pipe the video feed in .sh but not with this method.

    Any advise?

  4. It’s hard to know exactly what your problem is, but in my experience the most common source of problems like this has to do with environment variables, especially things like $HOME, or $PATH, or other such things that might be set when you run your .sh script but aren’t set (or are set differently) when run out of systemd.

  5. No, it should be /etc/systemd/system, not /lib/systemd/system.

    The service files in /lib/systemd are for packages you download i.e. your package manager installed them. System files added by local administrators use /etc/systemd. That way it keeps everything organized, not to mention all the service files in /etc have precedence over /lib files.

  6. Thanks – I put a note about this up top on the article and left the rest as-is because I don’t have an old jessie system handy to test it on at the moment. But you are clearly correct for current systems (and almost certainly correct for the jessie release I just haven’t verified that). I see that (thankfully) google has a lot more sources of answers to these types of questions now than it did back than.

Comments are closed.