Script doesn't work as a systemd service

Hey there,

I’m trying to make a script to update the Zellij configuration file whenever I toggle the dark mode on Gnome. It works as expected if I run it myself, however as a systemd unit despite the service appear to be running and I can see the process on htop it just doesn’t work.

script:

#!/bin/bash

gsettings monitor org.gnome.desktop.interface color-scheme | sed -E -u \
-e “s/.+‘(\w±)?(\w+)’/\2/” \
-e “s/default/light/” | \
while read mode; do
sed -i -E “/^theme .+/s/\w+"$/$mode"/” $HOME/.config/zellij/config.kdl
done

Unit file:

[Unit]
Description=Monitors gsettings change to color-scheme and updates the color-scheme on other programs

[Service]
Type=simple
ExecStart=/bin/bash /path/to/script
User=me

[Install]
WantedBy=multi-user.target
1 Like
mkdir -p -Z ~/.local/bin
tee ~/.local/bin/color-scheme-sync << "EOF" > /dev/null
#!/usr/bin/bash
gsettings monitor org.gnome.desktop.interface color-scheme \
| while read -r COLOR_SCHEME
do case "${COLOR_SCHEME}" in
(*default*) THEME="light" ;;
(*) THEME="dark" ;;
esac
sed -i -e "/^theme\s/s/\S*$/\"${THEME}\"/" \
"${HOME}"/.config/zellij/config.kdl
done
EOF
chmod +x ~/.local/bin/*
mkdir -p -Z ~/.config/systemd/user
tee ~/.config/systemd/user/color-scheme-sync.service << EOF > /dev/null
[Unit]
Description=Color scheme sync service
[Service]
ExecStart=%h/.local/bin/color-scheme-sync
[Install]
WantedBy=gnome-session.target
EOF
systemctl --user daemon-reload
systemctl --user enable color-scheme-sync.service
systemctl --user restart color-scheme-sync.service
2 Likes

As the script refers to $HOME, it should be run in a user context, that is, the unit file should be installed in /etc/systemd/user directory. The “WantedBy=” should be “default.target” as “multi-user.target” is only defined in the “system” context.

1 Like