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.
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.
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
See scheduled shutdown
If you want to check if there are scheduled shutdowns, there's a --show
tag for that.
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!"
To know more about other options with the shutdown command, do visit this article to know more in detail:
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.
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.
The syntax of each line in a cron file is explained below:
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:
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".
You can save and exit the file. The cron job will be automatically updated and will be executed strictly at 10 PM every day.
/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:
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:
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
)
Reload the daemon after creating both files:
sudo systemctl daemon-reload
Enabling the timer
Finally, go to the terminal and enable the timer with this command:
After enabling and starting the timer, you must see it in the list of timers given by
systemctl list-timers
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.
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.
A Computer Science student who happens to love Linux distributions. Loves programming and stands with spending 3 hours trying to automate things instead of doing it by hand.