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.

date

 

 

Use the data command to view the current time and date of the Ubuntu server

 

 

Example results of the date command on Ubuntu server

 

 

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

 

 

sudo at time - At command format

 

 

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

 

 

Enter your password - Related to the sudo command to restart the Ubuntu system

 

 

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

Hit enter

 

 

we can now enter the command that we want to execute

 

In this example we will restart the ubuntu system with the at 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.

 

 

You could now enter some other commands that you want to execute next

 

 

Hit CTRL + D

 

 

job has been scheduled - Note the Job Number (Example: 1)

 

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. 

sudo atq

 

 

sudo atq displays the at commands that were scheduled using with the sudo command

 

 

You can see that I have job number 3 set to run at 22:42 on April 4th

 

 

Here is an example of displaying a job scheduled to run at April 4th at 22:42

 

 

 

 

 

 

 

 

 

 

 

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. 

sudo atq

 

sudo atq - displays at jobs created using sudo

 

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

 

 

sudo at -c 4 to display the commands related to job 4 at command

 

 

You should see a bunch of enviroment information as well as the commands located towards the bottom of the output. 

 

Example output of the at -c job number command

 

 

 

 

 

 

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

 

 

sudo atrm job number - To remove a scheduled at job created using sudo

 

 

Now to test we can run the atq command again to make sure it has been removed .

sudo atq

 

 

No at jobs scheduled

 

 

Nothing was returned meaning nothing is in the que. 

 

 

Other Resources:


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!

http://www.youtube.com/watch?v=1xZomRM79xE

 

 

 

Examples of some different Date options and shortcuts to use with the at command:

http://content.hccfl.edu/pollock/unix/atdemo.htm

 

Command to start and stop mysql and apache on Ubuntu:

http://mixeduperic.com/ubuntu/how-to-start-stop-and-restart-apache-and-m...