In this article we explain how to make your system automatically shutdown using the Task Scheduler. And by this method you can make sure that your system/server will automatically shut itself down after a specific time, this can be done using the Task Scheduler.
If your server has not been rebooted for a long time (Linux: "uptime" command, Windows: Task manager, "Performance" tab), it is advisable to reboot your server one additional time before the actual maintenance. This is useful to prevent a long boot time after the maintenance itself, due to installation of updates or filesystem checks.
Instead of using a cronjob we can use the "at" command which helps to schedule a one-time execution at a particular time frame. Depending on your OS, you may need to install it. This can be installed via your distributions package manager, the package name is typically named "at". When this package is installed, a separate daemon "atd" is also installed, which takes care of monitoring and executing the scheduled tasks.
To shutdown the system in a proper way you should log onto the system as root user and issue the following command. This means that the system will shutdown all running programs and then halt.
root@localhost:~# shutdown -h now
And for scheduling a shutdown at a particular time frame in the future for example, schedule the shutdown to take place on 17 November 2018 at 16:00 local time on the server by issuing the following command.
root@localhost:~# echo "shutdown -h now" | at 16:00 Nov 17 2018
warning: commands will be executed using /bin/sh
job 1 at Mon Nov 17 16:00:00 2014
To check the schedule run in which you can display all scheduled tasks via the "atq" command.
Scheduling a shutdown action on Windows system is a bit more complex. To achieve this, it is recommended to use of the built-in Task Scheduler.
1. Open the Task Scheduler (Control Panel > Administrative Tools > Task Scheduler)
2. From the Action menu, select "Create Basic Task"
a. In the "Create a Basic Task" window, give the task a name, for example, "Shutdown for Maintenance"
b. Trigger: One time
- Start - Specify here the date and time local to the machine when the shutdown action should take place.
c. Action: Start a Program
- Program/Script: Specify here the full path to the shutdown.exe file: C:\Windows\System32\shutdown.exe
- Add arguments: /s (this is the switch to specify that a shutdown action should take place)
d. Verify on the Finish screen the details you have entered, especially the values in the Trigger and Action fields.
3. The new task will appear in the Task Scheduler Library from where you can modify the task or remove it if necessary.