Home » 2016 » April » 14 » AT command in Linux : schedule a task at later time

2:54 PM
AT command in Linux : schedule a task at later time

AT command is used to schedule/run a command at a later time, 1 hour later, 2 mins from now, 1 week later or one month later.

FORMAT
       at [-V] [-q queue] [-f file] [-mldrbvt] TIME
       at -c job [job...]
       atq [-V] [-q queue]
       atrm [-V] job [job...]
       batch [-V] [-q queue] [-f file] [-mv] [TIME]


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

atq     lists the user's pending jobs, unless the user is the superuser; in that case, everybody's jobs are  listed.

The format of the output lines (one for each job) is: Job number, date, hour, job class.

Example:

10      2016-04-12 17:07 a shankys


 

 For  example,

  • To run a job at 4pm three days from now, you would do at 4pm + 3 days,
  • To run a job at 10:00am on July       31, you would do at 10am Jul 31 
  • To run a job at 1am tomorrow, you would do at 1am tomorrow.

       The exact definition of the time specification can be found in /usr/share/doc/packages/at/timespec.

The  superuser may use these commands in any case.  For other users, permission to use at is determined by the files /etc/at.allow and /etc/at.deny.

       If the file /etc/at.allow exists, only usernames mentioned in it are allowed to use at.


Options:

 -m      Send mail to the user when the job has completed even if there was no output.

This option is required when you want a mail even if there is no output of the command on the stdout. If the output of command is redirected to a file so we get nothing on the stdout.

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

 


ATQ and at -l are the same. They are used to see the schedued jobs.

shanakys@localhost:~> atq
10      2016-04-12 17:07 a shanakys

shanakys@localhost:~> at -l
10      2016-04-12 17:07 a shanakys


Lets schedule a job to run after 1 minute from now. Here we use stdin for the commands.

shanakys@localhost:~> at now + 1 min
warning: commands will be executed using /bin/sh
at> date
at> cal
at> pwd
at> <EOT>
job 19 at 2016-04-14 11:47

Please note that you have to press 'ctrl+d' to end the at prompt.


Now we can see if the command is executed or not. Check your mails. Use mail command


Message  2:
From shanakys@localhost Thu Apr 14 11:47:02 2016
X-Original-To: shanakys
Delivered-To: shanakys@localhost
Subject: Output from your job       19
To: shanakys@localhost
Date: Thu, 14 Apr 2016 11:47:01 +0200 (CEST)
From: shanakys@localhost(shankys enterprizes. PL_073188)

Thu Apr 14 11:47:01 CEST 2016    #<-- date coammand executed
     April 2016     #<-- cal command executed
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

/home/shanakys   # <-- pwd command executed

If you want to delete a task scheduled using "at", you can use atrm or at -d with the jobs number

shankys@localhost:~> atq
10      2016-04-12 17:07 a shankys

 

Lets delete this job.


shanakys@localhost:~>
shanakys@localhost:~> atrm 10
shanakys@localhost:~> atq

(no jobs)

Lets schedule jobs using a file.

We create a file containing some commands

shankys@localhost:~> cat myat.txt
hostname
date
uname -a

/path/to/script1.sh

Now we use this file to schedule jobs using -f option.

shankys@localhost:~> date
Thu Apr 14 12:08:01 CEST 2016

shankys@localhost:~> at -f myat.txt 12:10pm today
warning: commands will be executed using /bin/sh
job 20 at 2016-04-14 12:10

shankys@localhost:~> atq
20      2016-04-14 12:10 a shankys

So as we see that the job is scheduled to run at 12: 10 PM today. We check mail to see if its executed or not.

Prompt>mail <ENTER>

Message  1:
From shankys@localhost  Thu Apr 14 12:15:01 2016
X-Original-To: shankys
Delivered-To: shankys@localhost
Subject: Output from your job       21
To: shankys@localhost
Date: Thu, 14 Apr 2016 12:15:01 +0200 (CEST)
From: shankys@localhost (SHANKYS ENTERPRIZES. PL_073188)

localhost    #<--hostaname command executed
Thu Apr 14 12:15:00 CEST 2016   #<-- date command executed
Linux localhost 3.0.101-0.7.29-xen #1 SMP Thu Jan 29 15:59:13 UTC 2015 (bbc501d) x86_64 x86_64 x86_64 GNU/Linux   # <-- uname -a command executed

 


At is really useful when you have to do something at later time for one time execution. If you want to schedule a re-occuring job, you should use crontab in linux.

  • 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 MMDDYY or MM/DD/YY or DD.MM.YY. 
  • 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.
     

 

 
 

Category: Open System-Linux | Views: 782 | Added by: shanky | Tags: schedule a job later, at command example, at command in unix, how to schedule a task at later tim, at command with example, at command in linux | Rating: 0.0/0

Related blogs


You may also like to see:


[2014-05-22][Open System-Linux]
Archiving and compression of files in Linux
[2014-10-25][Open System-Linux]
XMLWF command in Linux to check/validate/parse an XML file
[2015-08-18][Open System-Linux]
How to open a PDF file or a Postscript file in Linux - Evince Document viewer?
[2014-10-26][Open System-Linux]
XMLLINT command in linux : a validating XML parser
[2016-04-14][Open System-Linux]
AT command in Linux : schedule a task at later time

Total comments: 0
avatar