Tuesday, December 04, 2001

Executing Commands at a Future time

Some times you might want to execute a command at a certain (future) time where you are not logged on in your computer. The program at allows you do to precisely this. The usage of this command is simple, you give a time and a series of commands to be executed, and when the time comes the system will do what you want. Only thing you have to be careful is that the output of some commands might be lost (although in many system you get an email with the output, if that output should have been written to your terminal/console). But if we forget that for the time begin (I will post later how to redirect output of commands to files) we can start our first trials with the at command.

First of all, how to call at. You have to give a time at which you want your commands to be executed. Here are a few examples (you can find more details in the manual page, man at); most examples are self-explanatory:

  1. at 4pm will execute at 4 in the afternoon/evening today if you give the at command before 4PM, tomorrow otherwise. An equivalent time is at teatime (there are other short names: midnight and noon)
  2. at 10:31am
  3. at 10:30pm Jul 31
  4. at 4pm + 3 days will execute at 4 afternoon/evening three days from today
  5. at 4pm 101005 will execute on the 10th of October of 2005

Once you have given the at command with a time you will get a prompt that looks like this: at> Enter here the commands you want to be executed.

To look at what jobs are waiting in the at queue you can run the program atq. The output will look like this:

4       Wed Sep 28 14:00:00 2005 a pablo

This means thre is a job to be executed at 2PM. The job id is 4.

If you want to remove a job you have to type its id. In the above example we could do:

atrm 4

In a future post I will explain how to use cron to run commands periodically.

No comments: