In Linux distributions we can schedule jobs in two different ways. One is using crontab and second is scheduling using at command. Main difference between crontab and at commands are by using crontab we can schedule the recurring jobs and using at command we can schedule one time jobs at particular time period.
In this tutorial we will discuss about scheduling the jobs using at command. Apart from at command we also have atq, atrm, batch commands which are the addon commands to at command. Please find the details about at, atq, atrm, batch commands below.
at – To schedule the onetime jobs at particular time period.
atq – To list the jobs scheduled by using at command.
atrm – To remove the jobs scheduled using at command.
batch – To schedule the onetime jobs whenever system load average is below 0.8 or the value specified.
AT Command options:
at command is used to schedule the onetime jobs at particular time period. We can mention the time period in the form of HH:MM, AM, PM along with hours, midnight, noon, teatime (4PM), MMDD[CC]YY, MM/DD/[CC]YY, DD.MM.[CC]YY or [CC]YY-MM-DD. The specification of the time must follow the specification of the time of the day. You can also mention time like now + time units where time units can be minutes, hours, days, weeks.
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 Oct 31, you would do at 10am Oct 31 and to run a job at 1am tomorrow, you would do at 1am tomorrow.
If you specify a job to absolutely run at a specific time and date in the past, the job will run as soon as possible. For example, if it is 10pm and you do a at 6pm today, it will run more likely at 10:05pm.
For at and batch commands, commands are read from the standard input or from the file mentioned with -f option to the commands.
Both at and batch commands will work with root user. For other users the access permission for these commands can be determined in /etc/at.allow and /etc/at.deny files.
How to Schedule a job at 10:30PM today:
To schedule a job like executing a command or executing a script in at command is like below. After mentioning the time period to schedule the job an at> command prompt will be displayed. In that command prompt we need to enter the commands or scripts to schedule. After entering the commands or script names or job names press Ctrl + D to save the configuration. After this job schedule message will be displayed with the schedule time.
$ at 10:30PM today
warning: commands will be executed using /bin/sh
at>
at> echo "Test line 1"
at> <EOT>
job 9 at Thu Oct 27 00:45:00 2022
How to check the scheduled jobs details:
To check the jobs scheduled by command we have another command that is atq means at jobs queue. You can use at -l command also to list the scheduled jobs. Below is the command format for atq.
$ atq
$ at -l
$ atq
9 Thu Oct 27 00:45:00 2022 a root
10 Thu Oct 27 12:45:00 2022 a root
11 Thu Oct 27 02:05:00 2022 a root
12 Thu Oct 27 03:05:00 2022 a root
13 Thu Oct 27 03:05:00 2022 a root
16 Thu Oct 27 01:30:00 2022 c root
Above output of atq command displays the details about scheduled jobs by command. It contains job id, scheduled time, queue type of the job (a for at jobs, b for batch jobs), scheduled user.
How to check scheduled jobs output:
Once scheduled, jobs will be executed based on the time period. After completing the job the output will be mailed to the particular user mail box who scheduled the job. For default and local mail servers the mail box is available in /var/spool/mail/<username>. In this output of the scheduled jobs will be logged.
Below mail output is for the job executed with echo “Test line 1” command. You can check the output as “Test line 1” in the end of the mail body.
$ cat /var/spool/mail/test_user
From test_user@localhost.localdomain Thu Oct 27 01:39:02 2022
Return-Path: <test_user@localhost.localdomain>
Received: from localhost.localdomain (localhost [127.0.0.1])
by localhost.localdomain (8.15.2/8.15.2) with ESMTPS id 29R5d10s036495
(version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT)
for <test_user@localhost.localdomain>; Thu, 27 Oct 2022 01:39:01 -0400
Received: (from test_user@localhost)
by localhost.localdomain (8.15.2/8.15.2/Submit) id 29R5d1WM036494
for test_user; Thu, 27 Oct 2022 01:39:01 -0400
Date: Thu, 27 Oct 2022 01:39:01 -0400
From: test_user <test_user@localhost.localdomain>
Message-Id: <202210270539.29R5d1WM036494@localhost.localdomain>
Subject: Output from your job 18
To: test_user@localhost.localdomain
Test line 1
How to schedule a job after 30 minutes from now:
To schedule a job after 30 minutes from now use + operator with required time count in at command as below.
$ at now + 30 minutes
warning: commands will be executed using /bin/sh
at> cat /test_user/reports.txt
at> <EOT>
job 19 at Thu Oct 27 10:50:00 2022
For suppose current time is 10:20 AM Oct 27 2022, then above command will run at 10:50 AM Oct 27 2022 which is exactly 30 minutes from now.
Check the above scheduled job details using atq command.
$ atq
19 Thu Oct 27 10:50:00 2022 a root
How to schedule a job after 2 days from now:
Below command will execute at 10:20AM Oct 29 2022.
$ at now + 2 days
warning: commands will be executed using /bin/sh
at> echo "Executing date command after two days"
at> date
at> <EOT>
job 20 at Sat Oct 29 10:20:00 2022
Check the above scheduled job details using atq command.
$ atq
20 Sat Oct 29 10:20:00 2022 a root
How to schedule a job for tomorrow:
To schedule a job at tomorrow use below command.
$ at 11:30AM tomorrow
warning: commands will be executed using /bin/sh
at> echo "Test line 1"
at> <EOT>
job 21 at Fri Oct 28 11:30:00 2022
Check the above scheduled job details using atq command.
$ atq
21 Fri Oct 28 11:30:00 2022 a root
Below are the sample time schedules we can use in at command to schedule the jobs.
$ at tomorrow
$ at noon
$ at next week
$ at midnight
$ at next Sunday
$ at Aug
$ at 1:40 AM tomorrow
$ at 4:30 AM Oct 28
$ at now + 10 minutes
$ at now + 40 minutes
$ at now + 1 hour
$ at now + 2 hours
$ at now + 1 week
$ at now + 2 weeks
How to remove scheduled jobs:
We can remove the scheduled jobs using atrm command. To remove the scheduled jobs we need job id. To get the list of scheduled job ids use atq or at -l command. From this output get the job id and pass it to the atrm command to kill the job.
Below command will remove the job id 10 from the scheduled jobs list.
$ atrm 10
We can use at -r also to remove the job from scheduling.
Below command will remove the job id 10 from the scheduled jobs list.
$ at -r 10
Check the updated scheduled jobs list using atq command.
$ atq
How to check the version of at, atq, atrm commands:
To check the version details use -V option as below.
$ at -V
$ atq -V
$ atrm -V
How to set queue type for jobs:
While scheduling jobs we can mention the queue type for the jobs. These queue types are from a to z and A to Z. For at schedule jobs default queue value is a, for batch schedule jobs the default queue value is b. Queues with higher letters run with increased niceness. The special queue “=” is reserved for jobs which are currently running.
$ at -q c now + 10 minutes
Check the queue type details in scheduled jobs list using atq command.
$ atq
16 Thu Oct 27 01:30:00 2022 c root
How to send mail to the user after completing the scheduled job:
To send the mail after completing the job even if there is no output of the job use -m option as below
$ at -m 10:30AM today
Never send mail to user:
For not sending any mail to the user after completing the job use -M option as below
$ at -M 10:30AM today
How to schedule the jobs by reading jobs from file:
By default at command will schedule the jobs by reading the commands or script names from standard input. We can pass the script names or commands to schedule from file. For this use -f option and pass the file name.
$ at -f job_details.txt 1:30AM today
List the scheduled jobs details in standard output:
After scheduled a job with commands or scripts a shell script will be created with these commands we are giving while scheduling. To check that shell script use -c option with the scheduled job id.
$ at -c 10
Please find the shell script for the above scheduled job below.
Below shell script is for the job executed with echo “Test line 1” command. You can check the output as “Test line 1” in the end of the script
. . . . .
SHLVL=1; export SHLVL
LOGNAME=root; export LOGNAME
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus; export DBUS_SESSION_BUS_ADDRESS
XDG_RUNTIME_DIR=/run/user/0; export XDG_RUNTIME_DIR
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin; export PATH
HISTSIZE=1000; export HISTSIZE
LESSOPEN=\|\|/usr/bin/lesspipe.sh\ %s; export LESSOPEN
OLDPWD=/root; export OLDPWD
cd /usr/bin || {
echo 'Execution directory inaccessible' >&2
exit 1
}
${SHELL:-/bin/sh} << 'marcinDELIMITER643733b0'
echo "Test line 1"
marcinDELIMITER643733b0
To check more information about at, atq, atrm, batch commands please check this page.