ailgychwiniadau awtomataidd yn systemd
debian, bwa, het feddal, tarddeiriau…
hwyl: balchgweinydd systemawgrymso i run a few servers for various things. most of them are debian, since they're shared with other people, which means that they mostly autoupdate – but they don't automatically restart updated services.
to be clear, this is the correct default for a server distro. the best-written software is crash-only, but the world runs on more than just well-written code. you can't just blithely reboot whenever there's a package update, you need to let the sysadmin decide how that happens.
but, like, what if it's a simple nginx webserver, and it can safely be shut down whenever?
in my case, i want my servers to reboot once a day or once a week, depending on the system. for my purposes, i wanted to use systemd timers, because cron
is old and gross, and systemd is… still old and gross, actually. i don't know where i was going with that. i wanted to use systemd timers because i felt like it.
this turns out to be quite simple! make one file named /etc/systemd/system/reboot.service
:
[Unit]
Description=Reboot the machine
[Service]
ExecStart=/usr/bin/systemctl reboot
Type=oneshot
and another named /etc/systemd/system/reboot.timer
:
[Unit]
Description=Weekly reboot Mondays at 9:00 UTC
[Timer]
OnCalendar=Monday *-*-* 09:00:00
[Install]
WantedBy=timers.target
there are a couple of things to twiddle for your installation: the title of the timer, the first line in the second file, and the OnCalendar
line.
the format is a little weird, described in detail here. the idea is that every single segment has to match the current time, for the thing to fire. this is a pretty simple timespec: it matches Mondays, any year/month/day, and exactly 9:00:00 utc.
once you're done fiddling with the file, you can turn the timer on with a simple systemctl enable --now reboot.timer
. if you make updates, don't forget to systemctl daemon-reload
!