Skip to content

Instantly share code, notes, and snippets.

@hkoba
Last active November 6, 2023 16:07
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save hkoba/e05c91ebdd4e2ffd1d79e78e76cf1ff5 to your computer and use it in GitHub Desktop.
Save hkoba/e05c91ebdd4e2ffd1d79e78e76cf1ff5 to your computer and use it in GitHub Desktop.
systemd service and timer for scheduled reboot.
[Unit]
Description=Scheduled Reboot
[Service]
Type=simple
ExecStart=/usr/bin/systemctl --force reboot
[Unit]
Description=Reboot Scheduling.
[Timer]
OnCalendar=*-*-* 01:30:00
[Install]
WantedBy=multi-user.target
@lwbt
Copy link

lwbt commented Mar 4, 2021

Why add a "service" when you can specify a unit in the timer itself?

[Unit]
Description=Reboot Scheduling.

[Timer]
OnCalendar=*-*-* 01:30:00
Unit=reboot.target

[Install]
WantedBy=timers.target

Am I missing something?

@hkoba
Copy link
Author

hkoba commented Mar 5, 2021

Hi! Thank you for your comment.

When I wrote above, I didn't understand I can write Unit in Timer section. I will try your code next week. Thank you!

@Xeboc
Copy link

Xeboc commented Sep 26, 2021

man 7 systemd.special

       reboot.target
           A special target unit for shutting down and rebooting the system.

           Applications wanting to reboot the system should not start this unit directly, but should instead execute
           systemctl reboot (possibly with the --no-block option) or call systemd-logind(8)'s
           org.freedesktop.login1.Manager.Reboot D-Bus method directly.

           runlevel6.target is an alias for this target unit, for compatibility with SysV.

@hkoba
Copy link
Author

hkoba commented Sep 29, 2021

Hi!

Thank you for pointing this. I didn't know it.

man 7 systemd.special

@AleXoundOS
Copy link

@lwbt, @Xeboc, thank you for comments. Could you please, share a complete example, which follows your remarks?

@Xeboc
Copy link

Xeboc commented Jul 5, 2022

@hkoba's original example is correct. My response is in response to @lwbt's question about using reboot.target

To complete the setup, put the 2 files here:

/etc/systemd/system

And tell the init system to use them:

systemctl daemon-reload
systemctl enable sched-reboot.timer

@Surfingnet
Copy link

If you want to store your custom services' files somewhere out of /etc/systemd/system to make it easier to change the reboot hour for instance, you can use:

cd /correct/path/to/your/two/files
systemctl link ./sched-reboot.service
systemctl link ./sched-reboot.timer

So there are sym links in the place of the service and timer, directing to your files stored where you want.
Then, as Xeboc said above, don't forget to run:

systemctl daemon-reload
systemctl enable sched-reboot.timer

And finally you can check that your service and timer are correctly registered with:

systemctl status sched-reboot.timer
systemctl status sched-reboot.service

You should have Triggers: ● sched-reboot.service on the timer, and TriggeredBy: ○ sched-reboot.timer on the service.

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