The information in this post was tested on Fedora 22 which reports
$ systemctl --version systemd 219
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
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
or try to be smart and use the systemd variables like ‘%h’
then when you run
$ systemctl --user status hw
you will get errors like
Sep 16 14:48:27 ws002.zomojo.com systemd: [/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: hw.service lacks both ExecStart= and ExecStop= setting. Refusing. Sep 16 15:39:36 ws002.zomojo.com systemd: [/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: 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