Our previous two guides, whose links are below, we covered how to schedule system tasks or jobs in Linux using the cron and anacron commands. This guide will focus on scheduling tasks using at command in Linux systems.
How to Schedule Cron Jobs/Tasks in Linux/Unix
Scheduling tasks using anacron in Linux/Unix
Schedule tasks using at command in Linux
at utility is used to schedule a one-time task. These are the tasks that are executed only once at a specific time.at reads commands from standard input or from the file that is specified using the -f option.
Jobs scheduled using at are executed by the atd service.
Scheduling tasks using at command
The at command ordinarily expects time as the argument.
at TIMEWhere TIME can be specified using keywords such as;
noon,midnightorteatime (4:00 PM),next week,tomorrow,teatime tomorrow,next tuesday,next Tue,next Tuesday,next month.
When executed with no commands or scripts passed to it, at commands runs interactively and expects you to enter the commands to execute from the prompt.
at 11 PM Dec 2
at>At the prompt, enter the command to run. For example;
at 11 PM Dec 2
at> echo "This is a test at Job > /tmp/test-at-job"at 11 PM Dec 2
at> df -hTWhen done typing commands, press Ctrl+D to terminate input. When you press Ctrl+D, you will see <EOT> at the command line.
at 11 PM Dec 2
at> df -hTYou can as well pass the commands to execute to at command from command line by specifying the file with commands using the -f option. For example, consider a file called mycommands.sh with two shell scripts
cat mycommands.sh
/home/me/myscripts/clean-tmp.sh
/home/me/myscripts/backup.shTo have at execute these commands next week the same day at midnight;
at -f mycommands.sh midnight next weekYou can as well use the standard input redirection symbol;
at teatime next month < mycommands.shYou can also pipe tasks/commands to at command. For example to create a notification using the notify-send command,
echo 'notify-send "You need to refill the gas"' -t 1000 | at 17:00This will send you a notification at 5 PM to refill the gas.
NOTE:
- For all the
atexamples that follows below, you can either pass the the commands or scripts or tasks/jobs to be executed to at command or you can interactively enter them.
To run the job one week from today, same time as when the at job was scheduled;
at next weekTo run the job at 4 PM tomorrow
at teatime tomorrowRun a job on next Tuesday, same time as when the job was scheduled.
at next tueRun the job next month, same date at midnight.
at midnight next month- using the keyword
nowplus a time period. For example, if to schedule a task to run 4 hours from now, use the time periodnow + 4 hours. - For example;
To run the job today at 4PM plus 2 hours, that is at 6PM
at 4PM + 2 hoursTo run the job one and half hour from now;
at now + 90 minutesTo run the job two minutes from now;
at now + 2 minutesTo run a job next on monday 2 hours after the same time when the job was scheduled;
at monday + 2 hours- using a time of the day either in
24-houror12-hourclock system. For example;16:00or4:00 PM. - For example;
To run the job at 5PM today;
at 5:00 PM or at 5 PM or at 17:00- using
time of the dayanddate.- The date can be specified in the format
DD.MM.YY,MMDDYYorMM/DD/YY. MONTH Date, YearorMONTH Date YearorMONTH Date. You can write the first three letters of the month e.gJanorjan.- For example,
To run the job on first october, year 2019 same time as when the job was scheduled
- The date can be specified in the format
at 12:30 PM 10/01/19 or at 12:30 PM 01.10.19 or at 12:30 PM 100119To run the job on 2nd Jan, 2020 same time as when the job was scheduled
at Jan 1, 2020 ot at Jan 1 2020To run the job at 11 PM on 2nd Dec, this year;
at 11 PM Dec 2List scheduled at jobs
At command has a utility called atq that can be used to list at jobs that are pending execution.
To simply list the at jobs, run;
atqIf there are any jobs pending execution, you will see them on the output;
atq31 Mon Dec 2 23:00:00 2019 a root
33 Sun Oct 6 16:00:00 2019 a root
32 Fri Sep 13 00:00:00 2019 a root
Similarly, you can use at command with -l option.
at -lDelete Scheduled at jobs
Scheduled at jobs can be removed using the atrm utility or by passing option -r to at command.
atr -r <JOB ID>or
atrm <JOB ID>For example, based on the output of the atq command above, you can remove at job with job number 31 using the command;
at -r 31or
atrm 31To remove all the jobs in the queue, simply obtain the job numbers and remove them as follows;
atrm $(atq | cut -f1)There are other options that are aliased to atrm command.
at -d <JOB ID>Print Listed At Jobs to STDOUT
To print listed at jobs to standard output, simply use the command;
at -c <JOB ID>For example, to cat the at job number 23 to standard output.
at -c 23Controlling Access to At
It is also possible to control who can run at jobs using the /etc/at.allow and /etc/at.deny files.
- only users listed in the
at.allowfile are allowed to useat - users listed in
at.denyfile are not allowed to useat.
Using batch command
Another command almost similar to at command is batch command. This command is used to schedule tasks that can only be executed when system load drops below 1.5 or any value specified in
Batch command does not accepts any parameters and runs interactively.
batch
at>For example to execute the script, /home/me/myscripts/clean-tmp.sh, simply run batch command and enter the script at the at prompt.
Press Ctrl+d once you are done typing commands;
batch
at> /home/me/myscripts/clean-tmp.sh
at> batch command is no longer maintained on most Linux/Unix distributions.
Read more about at command on man at.


Hi,
Very useful article
Thanks a lot