Writing Systemd Service Units

I thought it would be great to have a central thread to compile information on systemd. Please feel free to post any examples of systemd units, or any other info relating to systemd on this thread.

The forum contains many different types of systemd units. If you are looking for a specific type of unit, I have categorized some common types to make examples easier to locate:


Network - WiFi:

Atheros ath10k

Atheros ath10k

Realtek rtl8812au

Network - Bluetooth:

Disable Bluetooth by Device ID

Network - Wired:

Realtek R8168

Broadcom BCM57762 (Thunderbolt to Ethernet)

Ethtool Ethernet configuration service

Network - General:

Suspend/Resume Network Service (basic/simple)

Systemd Mount Units:

NFS Mount Units

NFS Mount Units

Video Related:

KDE Plasma Restart Service

KDE Kwin Compositor Restart Service

Systemd Timers:

Timer - misc

Media library timer

Miscilaneous Services:

Media library service

Clean-n-sleep service

Autoclean Service

i3 screen lock service

Screen Lock Services

USB Device Restart Service

Disable/Enable a device by USB bus path

USB Restart Service

USB Mouse & Keyboard Wake Up Service

Disable Wakeup Events

Hard Drive Idle Service

Disable tlp on suspend service

Touchpad suspend service (psmouse)

Here are further links with systemd information:

The ArchWiki - systemd

Red Hat - systemd-targets

Red Hat - systemd unit files

Systemd manpage

Systemd Predefined Targets

System freezing during sleep/suspend
Kernel 4.19.0-3. Not Network after suspending - Gnome Edition
Bluetooth preventing suspend
Trackpad and WIFI are disabled after waking HP laptop from Suspend
Bash script not completing in Application autostart
Bash script interrupt for lid closed
Slow network Awaking from Suspend
Internet Connection Preventing Computer From Suspending
[SOLVED] WLAN interface -how to disable auto activation
Laptop won't suspend
[SOLVED] Laptop cannot resume from sleep
Suspend stopped working since a few days
Regression in kernel 4.19.6 prevents my laptop to suspend
[SOLVED] QCA6174 freez after suspend
Surface Pro 1796 Wifi not resuming after suspend
Having trouble setting up usb wificard
Hardware issues after suspending/sleeping laptop
[Solved] Network Manager & ? Systemd problems after resume from suspend
[Solved] Network Manager & ? Systemd problems after resume from suspend
Kernel 4.19.0-3. Not Network after suspending - Gnome Edition
(Solved) Lan not working at boot (JMicron PCI Express Gigabit Ethernet Adapter)
SABnzbd, Sonarr, and NAS
How to fix lock screen after changing gdm to LightDM
How to fix lock screen after changing gdm to LightDM
I can neither suspend or hibernate...I cannot wake up
Keyboard and mouse not working after suspend
USB keyboard not detected during early userspace with autodetect and keyboard hooks present
HP Probook 430 G6 Touchpad issues
Some devices failed to suspend, or early wake event detected
[R8168] Wired network connection fails to connect after resume (from suspend)
sysctl "dev.cdrom.autoclose=0" problem
Autostart script doesn't work
Make 'Interference between Headphones and Mouse' Solution Permanent
New session button in KDE creates a blank session
Wifi unavailable after computer suspends
Dual monitor issue when going into power save (monitor off) and back.
Prevent programs from blocking suspend mode
MacBook 12, 2017 Challenge: The quest for drivers
Wifi issues on a Macbook Air
how to use crontab as root
How to execute commands after hibernation?
R8168 parameter s5wol ignored
Use closed laptop as a desktop with another monitor
Closing lid cause network deactivation
[SYSTEMD BUG?] Can't suspend after suspending one time after fresh boot
Wireless USB keyboard/mouse not responding
Vetzki's PRIME tutorial discussion
Video/Display Glitch on All Kernels above 4.14

Originally when I started writing units I read that the units should be made executable. As scripts need to be executable that made perfect sense to me. However, after more research I found that is not required for systemd units, and it is erroneous to set them as executable.

This basic stuff that I have gathered so far:

Recommended Permissions For Systemd Unit Files

System-wide service units should be located in:


Systemd unit files (and any associated scripts) should have permissions set to 0644 (rw- r-- r–).

Recommended Directories For Systemd Unit Files

Directory For Systemd Unit Files Executed At The System Level.

The recommended location to put “system-wide” unit files is:


This is the preferred location for the system administrator to add new system-wide services. Unit files found in this directory location take precedence over any of the other locations on the file system. If you need to modify the system’s copy of a unit file, putting a replacement in this directory is the safest and most flexible way to do this.

Directory For Systemd Unit Files Executed At The User Level:

The preferred location to put “USER” unit files is:


You should also enable lingering for any user services that need to be available at startup.

loginctl enable-linger $USER

Recommended Directories For Systemd Executed External Scripts

Recommended Directory For External Scripts Executed At The System-Wide Level:


Scripts located in this directory can only be run by root as it is the system binary directory.

Recommended directories for external scripts executed at a user level by systemd units

One location that can be used for scripts to be run by all “users” is:


The preferred location to store scripts that are run as a standard user by systemd is:


There was a recent multi-post series on (i think) linux.com on systemd units and such.
I find them pretty straight-forward … but its always good to have nice docs around.
Lemme see if I can find it… ah there we go :

  1. Basic Services: Writing Systemd Services for Fun and Profit
  2. More Advanced Services: Beyond Starting and Stopping
  3. Device-aware services: Reacting to Change
  4. Paths: Monitoring Files and Directories
  5. Timers 1: Setting Up a Timer
  6. Timers 2: Timers: Three Use Cases
  7. Sockets: systemd’s “Socket” Units

Here’s a nice example of a new service file I just wrote for @kdemeoz that she just tested out a few mins back. This was successful in fixing her network resume issues.


#sudo systemctl enable network-off.service
#sudo systemctl start network-off.service
#sudo systemctl status network-off.service
#sudo systemctl daemon-reload
Description=Turn network off at suspend

ExecStart=/usr/bin/systemctl stop NetworkManager
ExecStart=/usr/bin/ip link set enp2s0 down
ExecStart=/usr/bin/modprobe -r r8169


#sudo systemctl enable network-on.service
#sudo systemctl start network-on.service
#sudo systemctl status network-on.service
#sudo systemctl daemon-reload
Description=Turn network on at resume

ExecStart=/usr/bin/sleep 20
ExecStart=/usr/bin/modprobe r8169
ExecStart=/usr/bin/sleep 3
ExecStart=/usr/bin/ip link set enp2s0 up
ExecStart=/usr/bin/sleep 3
ExecStart=/usr/bin/systemctl start NetworkManager



Arguably one of my favourite features in Linux. Mostly use it to launch my docker containers. :smiley:

1 Like

Who always thinks that up?
Installed in pamac as “systemd-ui” = Terminal = no
In the Whisker-menu you can find it as “systemadm” = Terminal = yes
Headline in the GUI = “systemd System Manager”
Please, please more corporate identity :wink:

really? edit: really.
I think I need simple examples (in german :wink: )
and mkdir -p ~/.config/systemd/user/ really user? or $USER = sgs?

Nice weekend and peace on earth :slight_smile:

ok, $ systemctl daemon-reload,
after reboot one example work. @xabbu :slight_smile: thanks.

I have a problem with that sleep thing. It is just hopefull thinking that the former process has finished after a while successfully. Every critical (actually all processes) should be evaluated for success or not. In your example you start with a sleep 20 and after loading the module another 3. Heck, why? If something like loading a module would take this much time there is seriously something wrong. If it does not load after 1 second it surely won’t after an hr. Thus I prefer a single script, that has to be executable, fired by the unit file, and this script can then unload its problems in the journal. BTW: The unit file itself does not have to be executable but the binaries and scripts it starts.
Just my thoughts

I didn’t think that all over. Is it not so, that systemd reads that unit file and fires one ExecStart after the other? So you would have a “sleep 20” running a “modprobe” then a “sleep 3” and so on. You cannot picture this commands as a script that executes the next command when the one before has finished. They are all hangin in there at the same time independent from each other.
Tell me, if i am wrong.

I will not argue your experiences calvous. I respect your opinion. I imagine hardware is a factor that makes something work on one machine, but not another.

I can only tell you from working on this stuff for a little while now with users on the forum you do run onto instances where the timing does factor into whether the modules are reloaded successfully.

Mostly reloading of modules will fail if an unload/reload is done without a time delay. Same with raising the adapter and loading the modules. Restarting network manager can also fail on a restart command without a delay between.

The 20 seconds delay is probably far more than required, but a lot of stuff is happening at once when coming out of suspend. I think a bit of a delay helps when bringing up the network again. In this instance I thought it best to start with a big delay so the service unit would be sucessful the on the first attempt. Some users get very frustrated if things don’t work right off the bat, then they throw in the towell, (when it would probably work if they just stuck with it). I informed the user after it was working that the times could probably be trimmed down, and left it to the user to do the fine tuning.

That was my reasoning, maybe I’m completely wrong. As it is, I’m the oly one on the forum helping users solve their suspend issues by writing a service unit. Im only just starting out, so its all a learning experience. Of course I’ll make some errors along the way , thats how we all learn. That’s also why I opened this thread, to get ideas, input, and feedback from others.

The commands are run one after the another. And only if the one before was successful.

A simple test.

$ cat /home/xabbu/.config/systemd/user/test1.service
Description=Test sleep

ExecStart=/usr/bin/sleep 20
ExecStart=/usr/bin/sleep 3
ExecStart=/usr/bin/sleep 3

started with systemctl --user start test1.service after a daemon-reload.

The output of systemctl --user status test1.service

● test1.service - Test sleep
   Loaded: loaded (/home/xabbu/.config/systemd/user/test1.service; static; vendor preset: enabled)
   Active: inactive (dead)

Okt 05 12:10:15 Tartaros systemd[1082]: Starting Test sleep...
Okt 05 12:10:15 Tartaros date[2075]: Fr 5. Okt 12:10:15 CEST 2018
Okt 05 12:10:35 Tartaros date[2100]: Fr 5. Okt 12:10:35 CEST 2018
Okt 05 12:10:38 Tartaros date[2103]: Fr 5. Okt 12:10:38 CEST 2018
Okt 05 12:10:41 Tartaros date[2109]: Fr 5. Okt 12:10:41 CEST 2018
Okt 05 12:10:41 Tartaros systemd[1082]: Started Test sleep.

Thanks, I frankly did not expect this. Much more stuff to read.

User output from the service file i posted. Everything fired off sequentially, (and worked beautifully, if i must say so myself).

kdemeoz@GA-Z97-HD3-Tower ~]$ systemctl status network-resume.service
● network-resume.service - Turn network on at resume
   Loaded: loaded (/etc/systemd/system/network-resume.service; enabled; vendor preset: disabled)
   Active: active (exited) since Fri 2018-10-05 12:15:46 AEST; 37s ago
  Process: 5455 ExecStart=/usr/bin/systemctl start NetworkManager (code=exited, status=0/SUCCESS)
  Process: 5411 ExecStart=/usr/bin/sleep 3 (code=exited, status=0/SUCCESS)
  Process: 5406 ExecStart=/usr/bin/ip link set enp2s0 up (code=exited, status=0/SUCCESS)
  Process: 5352 ExecStart=/usr/bin/sleep 3 (code=exited, status=0/SUCCESS)
  Process: 5344 ExecStart=/usr/bin/modprobe r8169 (code=exited, status=0/SUCCESS)
  Process: 4899 ExecStart=/usr/bin/sleep 20 (code=exited, status=0/SUCCESS)
 Main PID: 5455 (code=exited, status=0/SUCCESS)
[kdemeoz@GA-Z97-HD3-Tower ~]$ 

No offense. You’ve asked for input. I just outlined my thoughts. Even though @xabbu showed me different on my second answer, I still would put it into one script. This way you have more control over things. But @gohlip already said it: I am OLD :sob:

1 Like

IIRC this is advised in systemd manuals as well. Better use scripts for sequential commands.

I actually find it far faster when testing to use a separate script as well. Far quicker to use a script that is user writable for testing purposes. The problem with external scripts is when you have ask a user to create separate scripts, and unit files, then set permissions for all (in the terminal), guess what happens. Their eyes glaze over at all the stuff they have to do, and they give up.

So, its kind a a juggling game to actually get users to do the work required to fix their issue. The more work, the more likely they’ll just give up and go back to Ubuntu.

They all want their issue fixed, but they just dont want to have to work for it. I wrote up a set of service units (no scripts) for a user a couple days ago. Waste of effort, too much work for him to even bother trying. That gets very frustrating.

1 Like

Common old chum…Oh…I mean young buddy.
Since this subject is still open, I’ll be brief.
I mean “Old World” thinking, not “Old” people. Okay? Happy?
Here, have a lollipop. :lollipop: Don’t cry. :grin:

1 Like

sniff, thank you…

1 Like

True justification, though I got this topic as learning/testing testbed…
All is right…

Absolutely, any thoughts on the subject are appreciated.

I wouldn’t trust that user; she’s bonkers.

1 Like