Brainfarts Linux Create scheduled reboot via systemd

Create scheduled reboot via systemd

These are the steps to create a scheduled reboot via systemd on Ubuntu.

Filenames and unit descriptions in the example are chosen arbitrarily and can be changed.

  1. Add a service unit that restarts the system. To do that, create a new file in /etc/systemd/system/sched-reboot.service and add the necessary configuration into it. The following service config will run a forced reboot through systemctl whenever started. [Unit] Description=Scheduled Reboot [Service] Type=oneshot ExecStart=/usr/bin/systemctl --force reboot
  2. Add a timer, that will start the reboot service on a schedule. Create a new file in /etc/systemd/system/sched-reboot.timer. The following config for the timer will run the reboot service daily at 4AM [Unit] Description=Reboot Scheduling [Timer] OnCalendar=*-*-* 4:00:00 [Install] WantedBy=multi-user.target
  3. (Optional) Run systemd-analyze verify /etc/systemd/system/sched-reboot.* to check for errors in the config files. If the return is empty, the files are in order.
  4. Enable and start the timer by running sudo systemctl enable sched-reboot.timer and sudo systemctl start sched-reboot.timer.
  5. (Optional) Run systemctl list-timers to see all active timers as well as their next and last start information.

This is an older question, but it comes up as search result and does not contain any info about systemd, so I will add an example on how to do scheduled reboots with systemd. Filenames and unit descriptions in the example are chosen arbitrarily and can be changed.

  1. create new file:  /etc/systemd/system/sched-reboot.service 
  2. [Unit] Description=Scheduled Reboot
  3. [Service] Type=oneshot ExecStart=/usr/bin/systemctl --force reboot
  4.  /etc/systemd/system/sched-reboot.timer.
  5. [Unit] Description=Reboot Scheduling
  6. [Timer] OnCalendar=*-*-* 4:00:00
  7. [Install] WantedBy=multi-user.target
  8. (
  9. Enable and start the timer by running:
  10. sudo systemctl enable sched-reboot.timer
  11. sudo systemctl start sched-reboot.timer.
  12. (Optional) Run systemctl list-timers to see all active timers as well as their next and last start information.

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Create Service in UbuntuCreate Service in Ubuntu

sudo nano rtlsdrp2000.service [Unit]Description=RTL-SDR MQTT to MariaDBAfter=network.target [Service] Type=simpleStandardOutput=file:/home/pi/RTL-SDR-P2000Receiver-HA/mqtt_to_mariadb.logStandardError=file:/home/pi/RTL-SDR-P2000Receiver-HA/mqtt_to_mariadb.logExecStart=/usr/bin/python3 /home/pi/RTL-SDR-P2000Receiver-HA//mqtt_to_mariadb.py [Install]WantedBy=multi-user.target sudo cp mqtt-mariadb.service /etc/systemd/systemsudo systemctl enable mqtt-mariadbsudo systemctl start mqtt-mariadbsudo nano rtlsdrp2000.service