Oct 172012
 

In a former article I’ve talked about the commands cron and crontab  that are the standard way to schedule recurring things on a Gnu/Linux system.

But sometimes you need to do one thing at a specific date and time for just one time, and for these tasks the best option is to use at, another way to use at is to run a command later when the computer won’t be busy.

Or another reason could be that you want to run a command that requires a lot of time to end and you have to disconnect from that server, at could be used in this situation, but for these task i suggest to take a look at the articles on how to run commands on background and the utility screen




So the idea of the at command is to facilitate “one time command scheduling”. In this sense it is complementary to cron which usually is used to schedule periodic jobs, let’s see its most common uses.

The at command is part of the set of four commands that includes at, batch, atq, and atrm commands:

  • at executes commands at a specified time.
  • atq lists the user’s pending jobs, unless the user is the superuser; in that case, everybody’s jobs are listed.
  • atrm deletes jobs, identified by their job number.
  • 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.

Basic usage of the command at

at prompts you for the command to run. After entering your command, press ctrl-D to finish, these are the main options you can use with the command at:

 at [-m] [-q queue] [-f file] TIME

And this is what they means:

  • -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. If a job is submitted to a queue designated with an uppercase letter, it is treated as if it had been submitted to batch at that time. If atq is given a specific queue, it will only show jobs pending in that queue.
  • -m Send mail to the user when the job has completed even if there was no output.
  • -f file Reads the job from file rather than standard input.
  • TIME 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. 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.

Examples:

While doing the test this was the exact date:

#date
Wed Oct 17 22:31:05 CEST 2012

So giving only the time of the day it will be scheduled for the next time that this time is reached, in this instance, I’asking to schedule a job for the 20.00 and so will be processed the following day in that time:

#at -f my_at_test.sh 20:00
warning: commands will be executed using /bin/sh
job 4 at Thu Oct 18 20:00:00 2012

While if i specify a time not yet come today it will be scheduled for that time on today:

#at -f my_at_test.sh 22:35
warning: commands will be executed using /bin/sh
job 5 at Wed Oct 17 22:35:00 2012

And this is another example, using the ctrl-D to finish to finish:

at midnight Friday
warning: commands will be executed using /bin/sh
at> cp -a /project/source/* /backup/source/^C
at> 
job 6 at Fri Oct 19 00:00:00 2012

View all the scheduled jobs using atq

You can use atq command or as alternative at -l, to display all jobs that are scheduled or currently running, the only option available with this command is -q to specify a particular queue.

#atq
3	Thu Oct 18 10:25:00 2012 a linuxaria
4	Thu Oct 18 20:00:00 2012 a linuxaria
6	Fri Oct 19 00:00:00 2012 a linuxaria

This output is not so useful in my opinion, as you don’t see what the job 3 or the others do.
To see the content of a job you can use

at -c jobnumber

this will give a long output showing the full environment of your command and the command itself:

#at -c 6
 
#!/bin/sh
# atrun uid=1000 gid=1000
# mail linuxaria 0
umask 22
SSH_AGENT_PID=1579; export SSH_AGENT_PID
XDG_SESSION_COOKIE=586bed7d66c5f3bf0810f6ce0000001c-1350495018.439467-8023144; export XDG_SESSION_COOKIE
WINDOWID=25165830; export WINDOWID
GNOME_KEYRING_CONTROL=/tmp/keyring-m0UVoj; export GNOME_KEYRING_CONTROL
USER=linuxaria; export USER
LIBGL_DRIVERS_PATH=/usr/lib/fglrx/dri:/usr/lib32/fglrx/dri; export LIBGL_DRIVERS_PATH
SSH_AUTH_SOCK=/tmp/ssh-DbbUjzXR1501/agent.1501; export SSH_AUTH_SOCK
USERNAME=riccio; export USERNAME
SESSION_MANAGER=local/mint-desktop:@/tmp/.ICE-unix/1501,unix/mint-desktop:/tmp/.ICE-unix/1501; export SESSION_MANAGER
DEFAULTS_PATH=/usr/share/gconf/default.default.path; export DEFAULTS_PATH
XDG_CONFIG_DIRS=/etc/xdg/xdg-default:/etc/xdg; export XDG_CONFIG_DIRS
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games; export PATH
DESKTOP_SESSION=default; export DESKTOP_SESSION
PWD=/tmp; export PWD
GNOME_KEYRING_PID=1424; export GNOME_KEYRING_PID
LANG=en_US.UTF-8; export LANG
MANDATORY_PATH=/usr/share/gconf/default.mandatory.path; export MANDATORY_PATH
MDM_XSERVER_LOCATION=local; export MDM_XSERVER_LOCATION
SHLVL=1; export SHLVL
HOME=/home/linuxaria; export HOME
LOGNAME=linuxaria; export LOGNAME
XDG_DATA_DIRS=/usr/share/default:/usr/local/share/:/usr/share/:/usr/share/mdm/; export XDG_DATA_DIRS
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-QWgLeCbkps,guid=b1c32f270937311ee26524ff00000031; export DBUS_SESSION_BUS_ADDRESS
MDMSESSION=default; export MDMSESSION
WINDOWPATH=8; export WINDOWPATH
MDM_LANG=en_US.UTF-8; export MDM_LANG
XAUTHORITY=/home/linuxaria/.Xauthority; export XAUTHORITY
COLORTERM=gnome-terminal; export COLORTERM
cd /tmp || {
	 echo 'Execution directory inaccessible' >&2
	 exit 1
}
cp -a /project/source/* /backup/source/^C

This is much better to understand exactly what a command will do.

Remove a scheduled job using atrm

Now we have seen the list of the scheduled at commands and optionally their contents, now it’s time to see how to delete them, for this we can use atrm command or at -d, to delete a particular job.

So to remove the job number 3 you just have to type

atrm 3

. this give no output on the command line, but if you run again atq, you’ll see that the selected jobnumber has been deleted.

Limit the use of at for some users

The /etc/at.allow and /etc/at.deny files determine which user can submit commands for later execution via at or batch. The format of the files is a simple list of usernames, one on each line. Whitespace is not permitted.

Root can always use at.

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

If /etc/at.allow does not exist, /etc/at.deny is checked, usually this file contains a long list of “system users” such as bin, backup, ftp or www-data

Popular Posts:

flattr this!

  One Response to “Manage planned tasks on Linux with the command at”

  1. I use at frequently: 1)with only 1.5 mbit of download bandwidth, downloading a linux ISO can take quite a while for me. So I do it when my local network has the least traffic, and I do it while I sleep. 2)recording radio shows using streamripper.

 Leave a Reply

(required)

(required)


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>