Skip to main content

Schedule a Shutdown in Linux Command Line

Learn to schedule a shutdown in Linux. Also explore routine shutdowns using cron jobs and systemd-timers.

Pranav Krishna

Got to shut down the Linux system but have to wait for a process to finish?

No need to wait idly. You can schedule the shutdown instead:

shutdown +N

This way, the system will be shut down after N minutes. You can also schedule the shutdown at a predefined time:

shutdown 15:30

The above command will turn off the Linux system at 15:30 next.

There are more ways to shut down the Linux system at some point in the future. In fact, you can also set up a recurring shutdown schedule using crontab.

I'll show you all these in this tutorial.

🚧
If you are doing this on a remote server accessed via SSH, please ensure that you have a way to turn it on again.

Schedule a one-time shutdown in Linux

You can use the shutdown command to schedule a shutdown after X minutes or at a certain time.

To shut down after an arbitrary number of minutes, you can do that by adding +minutes to the command.

shutdown +15

The above command will schedule the shutdown after 15 minutes.

Shutdown was scheduled for 15 minutes
Shutdown scheduled for 15 minutes

If you would like to shut the system at 4:45 pm, you can add the timestamp (24-hour format) instead of counting the number of minutes :)

shutdown 16:45
shutdown -h 16:45

See scheduled shutdown

If you want to check if there are scheduled shutdowns, there's a --show tag for that.

shutdown --show
Check scheduled shutdowns with shutdown --show
Check scheduled shutdowns with shutdown --show

Cancel a scheduled shutdown

Changed your mind and don't want t

To cancel a shutdown, use the command:

shutdown -c

Bonus: Send a warning message to logged-in users

To send a message to every logged-in user, you may use the last option: the message. It uses wall to send the message to every user who's logged into a TTY interface.

Here's an example:

sudo shutdown +10 "Save your work! This system will shut down in 10 min!"
Example message with the shutdown command
📋
The given text will be wrapped in such that there are only 79 characters per line. Also, the message appears in the TTY interface every minute after 
Message shown in TTY every minute after execution of command
The message is shown in TTY every minute after the execution of the command

To know more about other options with the shutdown command, do visit this article to know more in detail:

Linux Shutdown Command: 5 Practical Examples
The shutdown command in Linux allows you to shut down, reboot or schedule a shutdown of your system. This article explains the most common and useful examples of the Linux shutdown command.

Scheduling shutdowns as routine

Routinely shutting down the system can be done in two ways. You can use the good old cron jobs or the newer systemd timers.

Using cron jobs

Cron jobs are known for scheduling tasks. It is popular and is found in Linux for a long time. Its peculiar syntax makes it an interesting concept to learn; do check out the detailed guide.

Crontab Explained in Linux [With Examples]
Learn the concept of crontab in Linux. See how you can set up cron jobs to automatically run scripts and command at predefined time.

Every user has a cron file that they can use to schedule stuff local to the user, but for systemwide applications like shutdown, you must use the root user's file.

Usually, cron files contain some text describing syntax and stuff (seen predominantly in Debian-based distributions) but don't worry if there's none.

Example cron file (Ubuntu) which explains syntax
Example cron file (Ubuntu) which explains the syntax and other stuff

The syntax of each line in a cron file is explained below:

Explanation of each field in a cron file
Explanation of each field in a cron file

Here, every field can contain certain values described above. An asterisk (*) can be used to denote 'all', meaning it will mean all values in that field. For example, if an * is given for the 'hour' argument, then the job is executed every hour throughout the day.

To start, open the cron file of the root user by:

sudo crontab -e

The assumption is that the system will shut down at 10:30 pm every day. Note that the time must be in the 24-hour format - 22:30 (and minute precedes hour).

To shut down the system every day (and for all months of the year), add this line at the end of the file:

30 22 * * * /sbin/shutdown now
Add this line at the end of the crontab file
Minute Hour Day in month Month in year Day of week Command
30 22 * * * /sbin/shutdown now

This translates to "At the 30th minute of the 22nd hour for all the days and months, execute the command /sbin/shutdown now to shut down the system".

Edit crontab as root (and check contents)
Edit crontab as root (and check contents) 

You can save and exit the file. The cron job will be automatically updated and will be executed strictly at 10 PM every day.

📋
It is mandatory to type out the entire/absolute path (/sbin/shutdown) of the command you are trying to execute. Typing just the command (shutdown) does not work with system files.

Using systemd-timers

Systemd timers are interesting. They are considered excellent alternatives to cron jobs! These files end with .timer extension which can control services or events.

There are two types of timers: Real-time (execute at this particular time at this frequency like cron jobs - depends upon clock) and Monolithic (initiating time is variable like boot time, etc)

For the purpose of shutting down a system at a particular time, real-time timers should be used, since they perform the action at the exact timestamp given.

shutdown.timer

To get started, create a timer file first:

sudo vim /etc/systemd/system/shutdown.timer

and you can paste this code inside the timer file:

[Unit]
Description="Timer to shut down the system"
Requires=shutdown.service

[Timer]
OnCalendar=*-*-* 22:30
Persistent=true

[Install]
WantedBy=timers.target
Contents of shutdown.timer

The Unit section comprises the properties of the timer (Description, required service, etc).

The Timer section tells us when and how the timer is run.

  • OnCalendar - the tag used to declare a real-time timer where scheduling is based on the calendar.
    Syntax: year-month-day hour:minute:second
    Here, the timer runs every day (*-*-* meaning every year, month or day) at the time 22:30 (or 10:30 pm)
  • Persistent - Defines if the timer should persist across reboots (true to enable the timer across reboots)

The Install section describes how the timer should be enabled.

  • WantedBy - used to specify different targets (default, timers, emergency, etc.) which are just a bunch of services grouped together. Since this is a timer, we use the timers.target.

shutdown.service

A timer works by triggering a service. A service for shutting down the system is necessary, hence create a service file with the same name:

sudo vim /etc/systemd/system/shutdown.service

Add the following contents to the file, save and exit:

[Unit]
Description="Service to Shutdown the system, run by a timer"

[Service]
Type=oneshot
ExecStart=/sbin/shutdown now

[Install]
WantedBy=default.target
Contents of shutdown.service

The Service section explains the behaviour of the service.

  • Type - describes the type of service (oneshot stands for a service that executes once and then terminates).
  • ExecStart - defines the command to execute when the service starts (here, it is a command to shut down the system: /sbin/shutdown now)
📋
Note that the names of the timer and service files must match in order for this method to work. It reduces confusion in deciphering which timer triggers which service.

Reload the daemon after creating both files:

sudo systemctl reload-daemon

Enabling the timer

Finally, go to the terminal and enable the timer with this command:

sudo systemctl enable --now shutdown.timer
This enables the timer at boot, and starts immediately too! 

After enabling and starting the timer, you must see it in the list of timers given by

systemctl list-timers 
List of all timers using systemctl list-timers
List of all timers (shutdown timer enabled)

From the highlight, it's noticeable that the timer has been initiated (which triggers a service), and a particular amount of time is left for it to execute.

Video demonstrating how to enable a systemd timer
Video demonstrating how to enable a systemd timer

Try to understand how systemd services work. Read through the files twice; you too can understand what each part does. Once you're comfortable, timers would be really interesting to work with.

I hope you found all the methods useful. Feel free to drop your doubts in the comments section if you have any.

Pranav Krishna