Skip to content

Instantly share code, notes, and snippets.

@spacecowboy
Last active February 26, 2024 08:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spacecowboy/596430332eb72545f73c4167d83c2586 to your computer and use it in GitHub Desktop.
Save spacecowboy/596430332eb72545f73c4167d83c2586 to your computer and use it in GitHub Desktop.
Systemd timer to restart systemd services on a schedule

First copy the files

Place the .timer and .service file in the /etc/systemd/system/ folder.

How to use

To use, let's assume I want to restart nginx every night. Then I'd do:

systemctl enable --now restart_nightly@nginx.timer

And that's it!

To verify you can type

systemctl list-timers

And it should report something like this:

NEXT                          LEFT                LAST                         PASSED             UNIT                         ACTIVATES
Thu 2021-03-25 05:00:00 CET   19h left            n/a                          n/a                restart_nightly@nginx.timer  restart_nightly@nginx.service
[Unit]
Description=Restarts %i every night
[Service]
ExecStart=/usr/bin/systemctl restart %i.service
[Unit]
Description=Restarts %i every night
[Timer]
OnCalendar=05:00
AccuracySec=10min
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment