Scheduling One Time Tasks With AT

Scheduling tasks is a great strength of Linux. Many administrative tasks such as scheduling backups, rotating log files, synchronizing time, etc. are done on a recurring basis with little to no user intervention using cron or anacron.

While cron and anacron facilities are great for recurring tasks, how can a user schedule simple tasks that only need to be run “one time” during the middle of the night or on a future date? The AT command is the answer. AT is one of those commands that a lot of people don’t know about but it can be very useful.

AT’s commandline syntax makes scheduling those “one time” tasks quick and easy. Let’s get started with some examples.

Example 1. – Create a new file on the desktop.

Open up a Termial or Console Window. At the command prompt type “at 2:00pm” and hit enter.

[sam@www Desktop]$ at 2:00pm

AT opens a prompt and waits for input. Since we want to create a new file, we’ll use the “touch” command along with the path where we want to create the new file on the desktop and then hit enter.
at> touch /home/sam/Desktop/newfile.txt

AT waits for more input. Press Ctrl-D on the keyboard to closes the prompt and display the job number and date it will execute.

at> <EOT>
job 1 at 2009-02-17 14:00
[sam@www Desktop]

If everything went right, you’ll see a newfile.txt on the desktop at 2:00pm. Pretty simple huh? To see a listing of scheduled jobs use the command “atq”.

[sam@www Desktop]$ atq
1 2009-02-17 14:00 a sam
[sam@www Desktop]$

If you decide to remove a job it’s simple as well using the command “atrm” plus job number.

[sam@www Desktop]$ atrm 1

Real World Example – Update a webpage at midnight.

Let’s say you build websites and a client asks you to update their website for a new promotion they’re having. The only catch is that they want page updated at exactly midnight. Believe me, this sort of request does and will happen. Not a problem with our new found AT abilities!

We create the new index.html page with the promotion info and save it somewhere on the server. I’m storing it in /home/sam/Desktop/index.html for this tutorial.

Open a terminal window and enter “at” along with the time specification. AT also allows for certain keywords to describe times such as noon, teatime, midnight, etc. Be sure to read the man page for more info. I’ll use the midnight keyword here.

[sam@www Desktop]$ at midnight

At at’s prompt we use the “cp” command to copy index.html to the website overwriting the current index page.

at> cp  /home/sam/Desktop/index.html   /var/www/html/testwebsite.com/index.html

Press Ctrl-D on the keyboard to closes the prompt and have the page updated at midnight. The client is happy, were getting our zzz’s, and at does the dirty work.

at> <EOT>
job 2 at 2009-02-18 00:00
[sam@www Desktop]

Conclusion
That’s about all there is to it. AT is simple, effective, and that’s the way it should be.


Tags: ,

 
 
 

Comments are closed.