Skip to content

Instantly share code, notes, and snippets.

@oprypin
Last active August 6, 2026 13:19
Show Gist options
  • Select an option

  • Save oprypin/0f0c3479ab53e00988b52919e5d7c144 to your computer and use it in GitHub Desktop.

Select an option

Save oprypin/0f0c3479ab53e00988b52919e5d7c144 to your computer and use it in GitHub Desktop.
systemd user unit + timer example

Save these files as ~/.config/systemd/user/some-service-name.*

Run this now and after any modifications: systemctl --user daemon-reload

Try out the service (oneshot): systemctl --user start some-service-name

Check logs if something is wrong: journalctl -u --user-unit some-service-name

Start the timer after this user logs in: systemctl --user enable --now some-service-name.timer

[Unit]
Description=Do some thing
After=network.target
[Service]
Type=oneshot
ExecStart=/home/my-user-name/my-service/script.py --some-arg=some-value
[Install]
WantedBy=default.target
[Unit]
Description=Run "Do some thing" every 30 minutes
[Timer]
OnBootSec=3min
OnUnitActiveSec=30min
[Install]
WantedBy=timers.target
@manuels

manuels commented Sep 19, 2022

Copy link
Copy Markdown

https://wiki.archlinux.org/title/systemd/User#Automatic_start-up_of_systemd_user_instances:

The systemd user instance is started after the first login of a user and killed after the last session of the user is closed. Sometimes it may be useful to start it right after boot, and keep the systemd user instance running after the last session closes, for instance to have some user process running without any open session. Lingering is used to that effect. Use the following command to enable lingering for specific user:

loginctl enable-linger $username

@gene1wood

Copy link
Copy Markdown

Should

  • systemctl --user daemon-reload
  • systemctl --user start some-service-name
  • systemctl --user enable --now some-service-name.timer

be run as root or as the user?

@joshbaptiste

Copy link
Copy Markdown

Should

  • systemctl --user daemon-reload
  • systemctl --user start some-service-name
  • systemctl --user enable --now some-service-name.timer

be run as root or as the user?

The user

@enjk-54L4

Copy link
Copy Markdown

Should
...
be run as root or as the user?

The user

Probably that's why it's called a user unit example ;-)

@enjk-54L4

Copy link
Copy Markdown

And for the actual directory to use you should inspect the output of pkg-config systemd --variable=systemduserconfdir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment