Linux Command Line: at

Linux Command Line: at
Page content

Why “at” not “cron”?

The main difference between at and cron is that cron schedules regularly occurring tasks whereas at schedules one-time executions of commands. The at command is very useful for scheduling tasks that need to be taken care of when you can not be present at your computer. The at command is also a very simple means of creating single instance reminders via email, popup, or sound.

Basic Usage

Using the at command is a bit different than using the standard command. When you use at it will be done from the at prompt to schedule an event. Also understanding how at deals with time is imperative. So let’s first examine time.

There are a number of ways you can schedule a timed event. You can use the exact time of day, you can use now + time increment, you can use day of the month + time of the day. Say, for instance, it is currently 1pm, and you want to schedule a task for 2pm that same day. You could either use: 2pm, now +60 minutes, or now +1 hour. Or say it is December 5 and you need to run a command (or application) on December 12. For that you could use december 12, now +7 days, or now +1 week.

Now that you have a basic understanding of time, let’s take a look at how the at command is used. Let’s schedule a print job to occur an hour from the current time. We’ll use the lp print command to print the file test.doc. The first thing to do is to issue the at command with the time necessary.The command is entered in a terminal window (such as aterm or eterm). After you enter the first line hit enter.

at now +1 hour

Your prompt will now look like:

warning: commands will be executed using (in order) a) $SHELL b) login shell c) /bin/sh

at>

At this point you will enter the command like so:

at> lp test.doc

When you hit enter you will see:

at>

Now hit the keys “Ctrl” and “d” at the same time to reveal output that looks similar to:

job 12 at 2008-12-05 14:24

You now have a job in the at queue. To check this you can issue the atq command which will print out the at queue. This will only print out job numbers. As you can see, in the sample text above, the job you just created is number 12. If you want to remove the job from the queue you can issue atrm which will remove all jobs from the at queue. If you want to remove only a specific job from the queue you would first find out the job number with the atq command and then issue atrm with the job number as an argument.

Another nice feature of at is the ability to mail to yourself the output of the command. For this you would use the -m argument. To get more output in the mail add the -v argument (for verbose). This would alter the initial command like so:

at -m -v now +1 hour

When this email arrives you should see the standard output as if you ran atq as well as the full command issued (atq does not list the actual command.)

Final Thoughts

The Linux at command is an incredibly useful tool. You can get creative and use it as a simple reminder. In the command section of your at prompt you could issue something like:

touch reminder; echo “pick up bread” > reminder; aterm -e nano /home/jlwallen/reminder

to pop up an aterm window with the nano editor showing the text “pick up bread”. Because you can string Linux commands together using the “;” character, your imagination is the only limitation to what you can do with the at command.

This post is part of the series: Linux Command Line

If you ever plan on doing any administration on a Linux machine, you would be well served to get to know the command line interface. In this Bright Hub series you will be introduced to various concepts surrounding one of the most powerful admin tools around.

  1. Linux Command Line: Introduction
  2. Linux Command Line: ls
  3. Linux Command Line: cd
  4. Linux Command Line: mkdir
  5. Linux Command Line: df
  6. Linux Command Line: ln
  7. Linux Command Line: top
  8. Linux Command Line: mount/umount
  9. Linux Command Line: Cron/Crontab
  10. Linux Command Line: chmod
  11. Linux Command Line: wget
  12. Linux Command Line: cat
  13. Linux Command Line: grep
  14. Linux Command Line: dd
  15. Linux Command Line: sudo
  16. Linux Command Line: startx
  17. Linux Command Line: adduser
  18. Linux Command Line: at
  19. Linux Command Line: aterm
  20. Linux Command Line: nano
  21. Linux Command Line: hostname