at command in Linux with Examples


Advertisements

Name

at - queue a jobs for later execution

Synopsis

at [-V] [-q queue] [-f file] [-mMlv] timespec...
at [-V] [-q queue] [-f file] [-mMkv] [-t time]
at -c job [job...]
at [-rd] job [job...]
batch
at -b

Description

at and batch command read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh. at command executes commands at a specified time.

batch executes commands when system load levels permit; in other words, when the load average drops below 1.5, or the value specified in the invocation of atd command.

at allows fairly complex time specifications, extending the POSIX.2 standard. It accepts times of the form HH:MM to run a job at a specific time of day. (If that time is already past, the next day is assumed.) You may also specify midnight, noon, or teatime (4pm) and you can have a time-of-day suffixed with AM or PM for running in the morning or the evening. You can also say what day the job will be run, by giving a date in the form month-name day with an optional year, or giving a date of the form MMDD[CC]YY, MM/DD/[CC]YY, DD.MM.[CC]YY or [CC]YY-MM-DD. The specification of a date must follow the specification of the time of day. You can also give times like now + count time-units, where the time-units can be minutes, hours, days, or weeks and you can tell at to run the job today by suffixing the time with today and to run the job tomorrow by suffixing the time with tomorrow.

For example, to run a job at 4pm three days from now, you would do at at 4pm + 3 days, to run a job at 10:00am on July 31, you would do at 10am Jul 31 and to run a job at 1am tomorrow, you would do run at 1am tomorrow command.

If you specify a job to absolutely run at a specific time and date in the past, the job will run as soon as possible. For example, if it is 8pm and you do a at 6pm today, it will run more likely at 8:05pm.

For both at and batch, commands are read from standard input or the file specified with the -f option and then executed.

Options

The options for at commands are:

-V    prints the version number to standard error and exit successfully.

-q    queue
      uses the specified queue. A queue designation consists of a single letter; valid queue designations range from a to z and A to Z. The a queue is the default for at and the b queue for batch. Queues with  higher letters run with increased niceness. The special queue "=" is reserved for jobs which are currently running.
-m    Send mail to the user when the job has completed even if there was no output.

-M    Never send mail to the user.

-f    file Reads the job from file rather than standard input.

-t    time run the job at time, given in the format [[CC]YY]MMDDhhmm[.ss]

-l    Is an alias for atq.

-r    Is an alias for atrm.

-d    Is an alias for atrm.

-b    is an alias for batch.

-v    Shows the time the job will be executed before reading the job.

Times displayed will be in the format "Thu Feb 20 14:50:00 1997".

-c    cats the jobs listed on the command line to standard output.

Examples

1. at command without any arguments allows jobs to be executed once in the future at a predefined date or time without editing any configuration file.

$ ping -c 5 google.com > ping.log | at now + 1min
warning: commands will be executed using /bin/sh
job 5 at Mon Jun  7 15:31:00 2021

We can see that the command has been queued, and its job id is 5, it also shows the time at which it would be executed.

We can see the ouput of the executed at command below.

$ cat ping.log
PING google.com (142.250.77.206) 56(84) bytes of data.
64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=1 ttl=117 time=3.77 ms
64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=2 ttl=117 time=2.98 ms
64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=3 ttl=117 time=3.02 ms
64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=4 ttl=117 time=2.98 ms
64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=5 ttl=117 time=3.14 ms

--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 2.985/3.184/3.776/0.303 ms

2. We can also use at command to read commands from standard input, which would be executed at a later time, using '/bin/sh'.

$ at 15:55
warning: commands will be executed using /bin/sh
at> ping -c 3 ebhashasetu.com > ebs.log
at> 
job 10 at Mon Jun  7 15:55:00 2021
$ atq
10	Mon Jun  7 15:55:00 2021 a expert
$ cat  ebs.log 
PING ebhashasetu.com (166.62.28.105) 56(84) bytes of data.
64 bytes from ip-166-62-28-105.ip.secureserver.net (166.62.28.105): icmp_seq=1 ttl=51 time=80.7 ms
64 bytes from ip-166-62-28-105.ip.secureserver.net (166.62.28.105): icmp_seq=2 ttl=51 time=77.0 ms
64 bytes from ip-166-62-28-105.ip.secureserver.net (166.62.28.105): icmp_seq=3 ttl=51 time=77.2 ms

--- ebhashasetu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 77.050/78.351/80.786/1.738 ms

3. We can see the list of jobs in the queue using atq command or use option -l which is an alias for atq command.

$ atq
3	Mon Jun  7 15:26:00 2021 = expert
4	Mon Jun  7 15:27:00 2021 = expert
6	Mon Jun  7 15:35:00 2021 a expert
$ at -l
3	Mon Jun  7 15:26:00 2021 = expert
4	Mon Jun  7 15:27:00 2021 = expert

We can observe, as the time elapsed, one of the job is executed and there is one job less in the queue.

4. We can use atrm command to remove a job from the queue, or we can use option -r or -d which is an alias for atrm command.

$ atq
3	Mon Jun  7 15:26:00 2021 = expert
4	Mon Jun  7 15:27:00 2021 = expert
$ atrm 3
Warning: deleting running job
$ atq
4	Mon Jun  7 15:27:00 2021 = expert

5. We can use -f option to read the jobs from the file rather than standard input.

$ at now +1 min -f cmd.sh
warning: commands will be executed using /bin/sh
job 15 at Mon Jun  7 16:25:00 2021
$ atq
15	Mon Jun  7 16:25:00 2021 a expert
$ ls -l all-sample.tgz 
-rw-rw-r-- 1 expert expert 181544 Jun  7 16:25 all-sample.tgz
$ cat cmd.log 
sample-file-3.txt
sample-file.txt
sample.txt
sample1.txt
sample3.txt
sample4.txt
Advertisements