systemd –user, a first look

Version

The information in this post was tested on Fedora 22 which reports

$ systemctl --version 
systemd 219

Two Modes

systemd runs in both system mode and user mode.  In a corporate environment this is amazingly useful because it means you can set up your own user services without having to hunt down a system administrator to set up system services.

Hello World as a bash program

In my ~/bin directory I wrote a simple bash hello world program that once every 10 seconds writes hello world to a file in /tmp/.  Make sure you chmod u+x ~/bin/helloworld.

$ cat ~/bin/hello-world 
#!/bin/bash

rm /tmp/hw.txt
while true; do
 DATE=$(date +%Y%m%dT%H%M%S.%N)
 echo $DATE hello world >> /tmp/hw.txt
 sleep 10
done

Unit file

systemd services are specified in “unit files”. User unit files are placed in ~/.config/systemd/user/.  My first mistake toying about with systemd user mode was that I replaced the characters “user” with my username. Those characters really are meant to be “user”.

$ cat ~/.config/systemd/user/hw.service
[Unit]
Description=Hello World

[Service]
ExecStart=/home/geoff/bin/hello-world

[Install]
WantedBy=default.target

Note that the command that is run by ExecStart must be specified as a full path.  If you do something like

ExecStart=~/bin/hello-world

or try to be smart and use the systemd variables like ‘%h’

ExecStart=%h/bin/hello-world

then when you run

$ systemctl --user status hw

you will get errors like

Sep 16 14:48:27 ws002.zomojo.com systemd[25899]: [/home/geoff/.config/systemd/user/hw.service:6] Executable path is not absolute, ignoring: %h/bin/hello-world
Sep 16 14:48:27 ws002.zomojo.com systemd[25899]: hw.service lacks both ExecStart= and ExecStop= setting. Refusing.
Sep 16 15:39:36 ws002.zomojo.com systemd[25899]: [/home/geoff/.config/systemd/user/hw.service:5] Executable path is not absolute, ignoring: ~/bin/hello-world
Sep 16 15:39:36 ws002.zomojo.com systemd[25899]: hw.service lacks both ExecStart= and ExecStop= setting. Refusing.

Interestingly, when you fix those errors by putting the full path in the command line, then rerun systemctl –user status hw, you will still see all the old errors.  Don’t go bananas trying to figure out why systemd has failed to reload your unit file, all is actually well.  You can see all is well by 1) noting that the timestamps on the errors are exactly the same as they previously were and 2) try starting the service.

$ systemctl --user start hw

Watch the service run via

$ tail -f /tmp/hw.txt

And stop the service by

$ systemctl --user stop hw
Advertisements
Tagged with: ,
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: