Systemd for user level

At one point, I got carried away making a bunch of user level services for Systemd. It’s a neat way to run scripts during user sessions providing cool functionality like handling dependencies and automatic restarts. One issue I ran into was that I couldn’t create units depending on system level targets. I wanted to have some user level units that depended on the, but I was blocked for a while. At some point I was introduced to dbus-monitor, which I immediately realized this could be used to build out monitor services to watch for system level events. I was able to create my own using the following scripts and services. I’m sure this could be simplified, but this works fine for my purposes :)


Description=User level sleep target


#!/bin/bashdbus-monitor --system "type='signal',interface='org.freedesktop.login1.Manager',member=PrepareForSleep" | while read x; do
case "$x" in
*"boolean false"*) systemctl --user --no-block stop;;
*"boolean true"*) systemctl --user --no-block start;;


Description=watch for sleep signal to start
ExecStart=/bin/bash -c watch_sleep

I use a which I start in my .zprofile after setting some env variables and adding them to systemd using dbus-update-activation-environment --systemd, but you could replace that with I also use /bin/bash -c to run the script since that will pick up my PATH, so you might need to change that with the full path.

Using a similar approach you could create some usable targets such using


#!/bin/bashdbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | while read x; do
case "$x" in
*"boolean false"*) systemctl --user --no-block start;;
*"boolean true"*) systemctl --user --no-block stop;;

You might be able to get some inspiration from my dotfiles ( If you got some other cool targets or units, please share in comments!

I’m Diego Fernández Giraldo, a Freelance Data Science Engineer looking to help your business succeed by ensuring you are leveraging data to its full potential.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store