How to write a script for scheduled reboot in Linux?

In Linux systems, you can use crontab to schedule tasks, including tasks for rebooting the system at specific times.

Here is an example of a Linux script for scheduling automated reboots, which can be set to run every day at 3 a.m. in the crontab.

  1. Create a script file: Run the following command in the terminal to create a reboot script file, such as restart.sh.
touch restart.sh
chmod +x restart.sh
  1. Edit the script file: open restart.sh with a text editor and input the following content:
#!/bin/bash
/sbin/shutdown -r now

Save and close the file.

  1. Set up scheduled tasks: edit crontab by running the following command.
crontab -e

Add the following content to the open file to schedule the reboot script to run every day at 3 AM.

0 3 * * * /path/to/restart.sh

Replace /path/to/restart.sh with the actual path of the script file.

  1. Save and exit: Press Ctrl + X, type Y to confirm saving, and then press Enter to exit edit mode.

The system will now execute a reboot script at 3 am every day to achieve the scheduled reboot function. Please note that scheduled reboots may affect running tasks and connections, so please set scheduled tasks carefully.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds