Both at and batch enable us to schedule one-off jobs for some time in the future.
The atd daemon should come by default with most Linux installations but if that was not the case we can install it with the command:
# yum install at
To start and enable use of the at scheduler we should use systemctl:
# systemctl start atd.service
# systemctl enable atd.service
The batch scheduler piggybacks the atd service, so by starting/enabling one we are in actual fact starting/enabling both.
To allow or deny at access to users, the same methodology as with cron’s cron.allow/cron.deny is used but this time with the files at.allow/at.deny . They work exactly the same way.
When a user that can schedule jobs with at does so, we can see the job details in /var/spool/at.
The syntax is flexible but it always work the same way: /usr/bin/at <TIME>.
# at now + 1 hour
at> <commands or script to run>
at> <commands or script to run>
at> Control + D
For example:
# at now + 1 hour
at> /usr/local/bin/gatherstats.sh > /var/log/gatherstats.log 2>&1
at> Control + D
So we call at and state the time we want it to run, and once inside the at prompt we enter the commands, scripts, etc that need to be run. When we are done, we press Control+D.
The time can be specified in a variety of ways:
# at 10:30pm -> if that time is past, the next day is assumed
# at 10:30pm tomorrow -> obviously 10:30 tomorrow
# at midnight -> next 00:00
# at noon -> next 12:00
# at teatime -> next 16:00
# at 10:00am 01/15/15 -> at 10:00am on the 15th of January 2015
# at 10:00am 01/15/2015 -> at 10:00am on the 15th of January 2015
# at 10:00am 15.05.2015 -> at 10:00am on the 15th of January 2015
# at 10:00am 20150115 -> at 10:00am on the 15th of January 2015
# at now + 1 minute -> in 1 minute
# at now + 2 hours -> in 2 hours
# at now + 3 days -> in 3 days
# at now + 4 weeks -> in 4 weeks
To check the possible time specifications it is better to read /usr/share/doc/at-<version>/timespec.
As said before can see the jobs pending by looking into /var/spool/at or by running atq or at -l . For example:
root:~> at -l
18 Wed Nov 4 09:44:00 2015 a root
19 Wed Nov 4 09:44:00 2015 a root
root:~> atq
18 Wed Nov 4 09:44:00 2015 a root
19 Wed Nov 4 09:44:00 2015 a root
We can delete pending jobs with atrm. For instance to delete the two scheduled jobs above we should run …
root:~> atrm 18 19
root:~> at -l
root:~>
Batch works the same as at but there is not time specification. Instead, we just state the script or commands that need to be run, and batch kick starts them when the system load falls below the 0.8 threshold.