Understand at, atq, atrm, batch Commands using 9 Examples

by SathiyaMoorthy on June 15, 2010

You can execute batch jobs in UNIX / Linux using any one of the three commands — at, batch or cron.

In this article, let us review how to schedule a job, view a job, and delete a job using at command.

You can schedule an at job in two different ways:

  • Schedule the job to be executed at a specific time. For example, July 3rd, 10AM
  • Schedule the job to be executed in relative time from now. For example, 5 hours from now.

1. Schedule an at job using specific date and time

Syntax:

$ at time date

For example, to schedule a job at 11 am on May 20, use the following at command.

$ at 11 am may 20

2. Schedule an at job using relative time

You can schedule a job to be executed using relative time from now.

Syntax:

$ at now + COUNT UNIT

For example, following job will be execute 1 minute from now.

$ at now + 1 min

The above example will read the commands from stdin, and it will execute the job after a minute. When you give something wrong in time format, you will get the error ‘Garbled time‘.

You can schedule a background job for 1 hour from now, (or) 1 day from now using the following at command:

$ at now + 1 hour

$ at now + 1 day

Similar to at command, you can also use crontab to execute jobs at a scheduled time. Refer to our earlier 15 cron command examples article.

3. View all the scheduled at jobs using atq

You can use atq command (or at -l), to display all the at command jobs that are scheduled or currently running.

The following atq command will list all the pending at jobs. The first number shown is the Job number, followed by the time in which the process is to be executed, and the user name.

$ atq
4	2010-04-20 11:00 a sathiya

4. Remove/Delete a scheduled at job using atrm

You can use atrm command (or at -d), to delete a particular job. For example, to delete the job number 4, use the following atrm command.

$ atrm 4

5. Execute a job only when system load average is < 1.5 using batch command

You can schedule a job using batch command, which will prompt for command input, which will be executed when the system load average is less than 1.5.

$ batch

At the successful completion of input, you will get job number. For listing and removing batch jobs you can use the at commands explained above.

6. Schedule at jobs from file using -f option

First create a text file that contains all the commands, or shell-scripts that you would like to be executed in the background using at command.

$ cat myjobs.txt
/home/sathiya/calculate-space.sh
/path/to/a/shell-script
/path/to/any/command/or/script

Using the -f option, you can make the at command to get the input from the file instead of stdin.

Following at command will execute all the jobs from the myjobs.txt 1 hour from now.

$ at -f myjobs.txt now + 1 hour

7. Allowing and Denying certain users from using at jobs

System administrator can control who can schedule an at job and who cannot using at.allow and at.deny files.

First, system checks for at.allow file. If at.allow exists, only the usernames specified in the at.allow file are allowed to use at command.

Next, (if at.allow doesn’t exist), system checks for at.deny file. If at.deny exist, the usernames specified in the at.deny file are not allowed to use the at command.

By default, most systems uses at.deny file to stop certain users from using the at command, such as www-data, guest, backup, man user.

8. Execute at command like nohup

Similar to the nohup command we discussed earlier, you can execute a command (or shell script) on the remote server using the at command and logout from the server.

$ at -f myjob now + 1 min

$ exit

Note: myjob will still be running even after you exit out of the server.

9. Additional at command time formats

You can use any one of the following at command date time formats:

$ at 10 am tomorrow

$ at 11:00 next month

$ at 22:00 today

$ at now + 1 week

$ at noon

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 14 comments… read them below or add one }

1 tanmay joshi June 15, 2010 at 6:29 am

Nice article.

Thanks,
Tanmay

2 Rajkumar Agrawal June 15, 2010 at 6:42 am

I like nohup section of this web page

3 kashu June 15, 2010 at 7:54 am

That’s what I need!
Thank you for your good article!

4 Russell June 15, 2010 at 10:39 am

What bugs me about at is that I can’t find any way to list the pending jobs.

5 Crouse June 15, 2010 at 12:54 pm

@Russell – did you read the article ?
————————– snip ———————————————–
3. View all the scheduled at jobs using atq

You can use atq command (or at -l), to display all the at command jobs that are scheduled or currently running.
————————– snip ————————————————

6 Russell June 17, 2010 at 3:18 pm

Ok yes you can get a list of very bare information using atq but it doesn’t tell you much. Let me say how I use at as follows:

I use at to send myself email reminders. Below is code I added to .bashrc

#add reminder function
function remind {
echo “format is: remind \”subject\” \”date\” – use e.g. 12 jan 10 to avoid US dates ”
echo “mail -s \”REMINDER $1\” myemailaddress@gmail.com < /home/me/remindermessage.txt" |at """01:00 $2"""
}

This allows me to easily set up a reminder at the terminal. Example if I type:
remind "Sues birthday tomorrow" "26 July 2010"
This will wait until 1am on 26 July then send an email to myemailaddress@gmail.com with the subject line "Sues birthday tomorrow". The reminder is just the subject line of the email so that it stands out in my list of emails. The file remindermessage.txt is the body of the email and contains "This is a reminder – see subject".

Ideally I would like to list the pending reminders I have set up as I might not remember if I set up a particular reminder. I haven't been able to find out how to use the atq listing to dig down and find the list of emails. Maybe someone can suggest a way to do this. (Of course I expect there's some software out there somewhere which can allow me to set up email reminders but no doubt the application contains hundreds of other features I don't want.)

7 Carlos Garcia June 23, 2010 at 9:31 am

How I can modify the minimun of load average in batch command?
Thank you

8 SathiyaMoorthy July 4, 2010 at 7:16 am

@Carlos

While starting the atd you can use -l option to do it. Refer man atd for further clarification.

9 Seshagiri September 21, 2010 at 4:23 am

How to know the job name scheduled by “at” command.

10 priti October 17, 2011 at 1:51 am

knowledegable article

11 alif June 15, 2012 at 4:46 am

I am using the “at” command to schedule tasks like below.

at -f resume.sh now + 2 min

I am able to see the tasks using the atq command. They seem to execute at the correct time since they are removed from the queue thereafter. But I don’t see the expected output.

I have tried many different combinations but to no avail.

I also tried the following:

at now + 2 min
at> echo “at running” > /home/user1/at_log.txt
at>

The atd process is running and the task receives a queue id., however the file at_log.txt is not generated.

Is there something I am missing ?
How can the atd be debugged ? Is there a log file for the commands executed by atd ?

Thanks in advance

12 Brian September 26, 2012 at 7:18 am

What I find very confusing with the ‘at’ commands is that not ALL of the running jobs show up in the ‘atq’ list…

For example, I run jobs using different queue names. Currently, my system is running a job in queue ‘j’, however, it does not show up in the ‘atq’ list. Also, it is NOT the ‘=’ job as this is a totally different job. This makes it very confusing to see what jobs are in the queue / running / etc. Any clues or is this a bug???

100691 Wed Sep 26 09:32:00 2012 t root
100744 Wed Sep 26 09:13:00 2012 z root
100745 Wed Sep 26 09:14:00 2012 e root
97568 Wed Sep 26 14:41:00 2012 z root
100701 Wed Sep 26 09:38:00 2012 z root
100751 Wed Sep 26 09:13:00 2012 t root
100529 Wed Sep 26 11:11:00 2012 v root
98605 Wed Sep 26 20:28:00 2012 z root
100702 Wed Sep 26 08:38:00 2012 = root
100710 Wed Sep 26 09:22:00 2012 v root
99365 Thu Sep 27 00:42:00 2012 d root
100750 Wed Sep 26 09:16:00 2012 z root
100752 Wed Sep 26 09:13:00 2012 z root

Any comments or is this a bug?

13 Maurice March 20, 2013 at 7:17 am

Is there a way to show the contents of a scheduled job?
I can see the list with at -l, but I would like to so what ie. job 3 will actually do?

14 Pavel September 5, 2013 at 10:17 pm

@Maurice

To see the contents of a scheduled job, type:

at -c

Example:
use “at -c 2″ to see the contents of job #2

at seems to store all your environment variables in the job itself and restores them when it runs, so you might get a lot of output. The command you actually entered should be on the last line(s).

Leave a Comment

Previous post:

Next post: