How To Schedule A One Time Restart on Your Ubuntu System Using The at Command
The at command allows you to execute a command or a set of commands at a defined time. It is similar to a cron job except it is used for running the command or task just once.
I will often use the at command to apply updates and restart my server at off peak hours.
In the following example we will schedule our Ubuntu server or desktop to restart at a defined time.
Let's get started by signing into the Ubuntu system via the console or by SSH.
Creating an At Command:
The first thing we will want to do is check the time to make sure it is correct . To check this we can use the date command.
You can see that it displays the date as well as the time.
Let's schedule the ubuntu system to reboot at 14:51 today. If the time has already passed for the day it will be schedule for the next day at that time.
Since we will be running a command that needs to have root privilages (restart), we will want to use sudo in front of the at command otherwise nothing will happen.
Note: Using sudo with the at command will have the job to run under root. To view and manage the job you will need to make sure to use sudo infront of the managment commands like atq, atrm and at -c.
sudo at 14:51
Note: If you get an error message saying the at command is not reconized you may have to install it by using the following command: sudo apt-get install at
Enter your password
Now enter the commands you wish to execute. In this example we will be restarting the system using the shutdown -r now command. The commands are the same as any other bash command.
shutdown -r now
You could now enter another command to execute after the first one has completed. Since we don't have any others to enter we can hit CTRL + D to finish.
Hit CTRL + D
You can see that it was assigned job number 1 and is scheduled to run at 14:51 Wed April 4th.
If you wanted to have the system reboot on april 10th at 1:00 am you could use the at command at 1:00 april 10
There are a lot of shortcuts like at now + 10 minutes (Runs 10 minutes from now), at now + 10 day (Runs at this time 10 days from now), at noon (Runs at noon) ...etc.
Check out http://content.hccfl.edu/pollock/unix/atdemo.htm for other great examples and shortcuts.
Viewing schedule at commands:
You can use the atq to list the at commands in que and what time it is scheduled for.
Note: If you enter the atq command it will display the jobs in que for the user you are running the atq command as. If you created your at command using sudo like in the example above you will need to put a sudo infront to of the atq command to be able to view it.
You can see that I have job number 3 set to run at 22:42 on April 4th
View command that will be executed with the scheduled at command:
If you would like to see what commands will be run under a specific job use the at -c job number command. The first thing you will need to know is the job number. This is displayed when the job is created or you can use the atq command to view the jobs and job numbers.
You can see above that the job number for my at command is 4 (first number on the line). To display the commands we can use the at -c job number command.
sudo at -c 4
You should see a bunch of enviroment information as well as the commands located towards the bottom of the output.
Removing schedule at commands:
In the example above my job number was 4. To remove it I would just use the atrm job number command:
sudo atrm 4
Now to test we can run the atq command again to make sure it has been removed .
Nothing was returned meaning nothing is in the que.
YouTube Video on explaining the at command:
Charles from http://networkingprogramming.com/ created a great youtube video explaining the at command. I would recommend check out his site for many other great video tutorials!
Examples of some different Date options and shortcuts to use with the at command:
Command to start and stop mysql and apache on Ubuntu: