How to PROPERLY delay reboot in Linux? [5 Methods]


Written by - Omer Cakmak
Reviewed by - Deepak Prasad

Linux is a powerful operating system that is widely used in various applications, ranging from servers to embedded systems. One of the common tasks that system administrators face is performing system updates and applying patches. However, rebooting the system after an update or patch installation may cause downtime for critical applications and services, resulting in lost productivity and revenue. In such cases, delaying the reboot becomes essential.

In this article, we will discuss the methods to delay reboot in Linux, including how to check for pending updates, configuring update policies, and scheduling reboot times. By following these methods, system administrators can ensure that updates are installed promptly without affecting the availability of critical services.

However, keep in mind that the delayed restart can be canceled entirely.

 

Different methods to delay reboot in Linux

You can delay the reboot with the shutdown and parameter, a commonly used reboot command in Linux. Or you can always and permanently delay the reboot process by editing the system.conf file. If a scheduled restart is required, we recommend reviewing the "at" command method. Finally, the reboot command and the sleep command can be used together. Let's exemplify these methods.

 

Method-1: Use the shutdown Command

The shutdown command is a powerful tool in Linux that allows you to power off, reboot, or halt a system. It can be used to schedule a reboot at a specific time, allowing you to delay the reboot until a more convenient time.

The basic syntax of the shutdown command is as follows:

shutdown [OPTIONS] [TIME] [MESSAGE]

Here's a breakdown of each part of the command:

  • OPTIONS: This part of the command specifies the action to be taken. The most commonly used options are -r (reboot), -h (halt), and -P (power off).
  • TIME: This part of the command specifies the time at which the action should be taken. The time can be specified in various formats, such as now, +5, or hh:mm. If no time is specified, the action will be taken immediately.
  • MESSAGE: This part of the command allows you to include a message that will be displayed to users before the action is taken. The message should be enclosed in quotes.

Here's an example of using the shutdown command to reboot a system in 5 minutes with a message:

$ sudo shutdown -r +5 "System will reboot in 5 minutes"
[sudo] password for foc: 

Broadcast message from root@ubuntu22desktop on pts/1 (Sat 2023-04-01 17:39:43 +03):

System will reboot in 5 minutes
The system is going down for reboot at Sat 2023-04-01 17:41:43 +03!

Reboot scheduled for Sat 2023-04-01 17:41:43 +03, use 'shutdown -c' to cancel.

In this example, the -r option specifies that the system should be rebooted, and the +5 specifies that the reboot should occur in 5 minutes. The message "System will reboot in 5 minutes" is included to provide a notification to users.

It's important to note that the shutdown command requires root privileges to execute, which is why sudo is used in the example above. Additionally, the shutdown command should be used with caution, as it can cause disruptions to running services and applications if not used properly.

If you want to cancel the shutdown, use the -c parameter.

$ sudo shutdown -c

Broadcast message from root@ubuntu22desktop on pts/1 (Sat 2023-04-01 17:53:45 +03):

The system shutdown has been cancelled

If the delay time is not canceled, the command starts to run and the information that the system will restart is written to the terminal every minute. After this time, the server restarts:

$ 
Broadcast message from root@ubuntu22desktop on pts/1 (Sat 2023-04-01 17:40:43 +03):

The system is going down for reboot at Sat 2023-04-01 17:41:43 +03!


Broadcast message from root@ubuntu22desktop on pts/1 (Sat 2023-04-01 17:41:43 +03):

The system is going down for reboot NOW!

Another method is to give a specific time in the shutdown command. For example, the command for reboot at 18:30 should be written like this:

// To specify a time of 10:30 PM, use:
sudo shutdown -r 22:30

// To specify a delay of 30 minutes, use:
sudo shutdown -r +30

// To specify a delay of 1 hour and 30 minutes, use:
sudo shutdown -r +90

// To specify a delay of 2 hours and 15 minutes, use:
sudo shutdown -r now +135
NOTE:
Use -h argument instead of -r to shut down the system and turn off power instead of reboot.

 

Method-2: Using DefaultTimeoutStopSec in /etc/systemd/system.conf file

In this method we will edit the system.conf file. Open the system.conf file with an editor:

sudo nano /etc/systemd/system.conf

Then find the line #DefaultTimeoutStopSec=90s and delete the # at the beginning. You can give this value in minutes or seconds:

DefaultTimeoutStopSec=60s

or:

DefaultTimeoutStopSec=1min

Then run the daemon-reload command:

sudo systemctl daemon-reload

Now reboot with the shutdown command:

$ sudo shutdown -r

Broadcast message from root@ubuntu22desktop on pts/1 (Thu 2023-03-30 21:43:27 +03):

The system is going down for reboot at Thu 2023-03-30 21:44:27 +03!

Reboot scheduled for Thu 2023-03-30 21:44:27 +03, use 'shutdown -c' to cancel.

You can see that the restart will start after 1 minute. You can use shutdown -c to cancel the reboot.

$ 
Broadcast message from root@ubuntu22desktop on pts/1 (Thu 2023-03-30 21:44:27 +03):

The system is going down for reboot NOW!

Connection to 192.168.122.75 closed by remote host.
Connection to 192.168.122.75 closed.

Now every reboot will be delayed for the time defined in the DefaultTimeoutStopSec variable in the system.conf file.

 

Method-3: Use the at Command

The at command is a useful tool in Linux that allows you to schedule tasks to be executed at a specific time. You can use this command to delay a reboot by scheduling it to occur at a later time, which can be useful if you need to perform updates or maintenance on the system, but want to delay the reboot until a more convenient time.

To delay a system reboot in Linux, use the following command to schedule the shutdown command to be executed at a specific time:

# echo "shutdown -r now" | at 18:43

This command schedules the reboot to occur at 18:43. You can adjust the time to suit your needs.

To check the scheduled tasks, use the following command

# atq
13 Tue Apr 4 18:43:00 2023 a root

When the scheduled time arrives, the system will be rebooted automatically.

# Connection to 192.168.122.75 closed by remote host.
Connection to 192.168.122.75 closed.

To view the scheduled task, use the following command:

$ atq
16	Wed Apr  5 18:42:00 2023 a foc

This command displays the content of the scheduled task with ID 15.

$ at -c 15
...
shutdown -r now
...

To remove a scheduled task and cancel the reboot, use the following command:

$ atrm 15

This command removes the scheduled task with ID 15 from the at queue.

 

Method-4: Use reboot end Sleep Commands

To delay a system reboot in Linux with a user or root user authorized to run the reboot command without a password, use the following command:

# sleep 5 ; reboot

This command delays the reboot for 5 seconds. You can adjust the time to suit your needs.

To delay the reboot using the shutdown command, use the following command:

# sleep 5 ; shutdown -r now
Connection to 192.168.122.75 closed by remote host.
Connection to 192.168.122.75 closed.

This command delays the reboot for 5 seconds and then initiates a reboot. When the specified time arrives, the system will be rebooted automatically.

When the specified time arrives, the system will be rebooted automatically.
After the reboot, the connection to the system will be closed.

 

Method-5: Use the cron scheduler

The cron scheduler is a useful tool in Linux that allows you to schedule tasks to run automatically at specific times. You can use this tool to schedule a reboot at a specific time in the future, which can be useful if you need to perform updates or maintenance on the system.

For example, you can use the cron scheduler to schedule a reboot at 2:00 AM every day. To do this, you can use the following command to edit the cron table:

crontab -e

This command will open the cron table in your default editor. To schedule a reboot at 2:00 AM every day, add the following line to the file:

0 2 * * * /sbin/reboot

This line specifies that the system should reboot at 2:00 AM every day using the reboot command.

After adding this line, save the file and exit the editor. The system will now automatically reboot at the specified time every day.

 

Bonus TIP

You can define an alias to the shutdown command to always delay the reboot for a certain amount of time:

$ alias reboot="shutdown -r +5"
$ alias 
...
alias reboot='shutdown -r +5'

This way when reboot command is executed it always happens +5 minutes later.

 

Summary

Delaying a system reboot in Linux can be useful when you need to perform updates or maintenance on the system without disrupting critical services or applications. There are several methods to delay a system reboot in Linux, including using the shutdown command, the systemd system manager, the at command, the sleep command, and the cron scheduler.

The shutdown command can be used to delay a reboot by specifying a time interval or a specific time. The systemd system manager provides a powerful tool for managing system services, process control, and scheduling, which can be used to delay a reboot by creating a timer unit and a service unit that will perform the reboot.

The at command allows you to schedule tasks to be executed at a specific time, which can be used to delay a reboot by scheduling it to occur at a later time. The sleep command can be used to delay a reboot by specifying a time interval before the reboot.

The cron scheduler can be used to schedule a reboot at a specific time in the future. By using the yum package manager, you can install packages such as at that can help you delay a reboot or schedule it for a later time.

Feel free to comment on the problems you encounter.

 

References

serverfault.com - How do I schedule a reboot on Linux?
www.redhat.com - How to schedule tasks using the Linux 'at' command

 

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on LinkedIn or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment