107.2 Lesson 2
Certificate: |
LPIC-1 |
---|---|
Version: |
5.0 |
Topic: |
107 Administrative Tasks |
Objective: |
107.2 Automate system administration tasks by scheduling jobs |
Lesson: |
2 of 2 |
Introduction
As you learned in the previous lesson, you can schedule regular jobs using cron or systemd timers, but sometimes you may need to run a job at a specific time in the future only once. To do this, you can use another powerful utility: the at
command.
Schedule Jobs with at
The at
command is used for one-time task scheduling and only requires that you specify when the job should be run in the future. After entering at
on the command line followed by the time specification, you will enter the at
prompt where you can define the commands to be executed. You can exit the prompt with the Ctrl+D key-sequence.
$ at now +5 minutes warning: commands will be executed using /bin/sh at> date at> Ctrl+D job 12 at Sat Sep 14 09:15:00 2019
The at
job in the above example simply executes the date
command after five minutes. Similar to cron
, the standard output and error is sent to you via e-mail. Note that the atd
daemon will need to be running on the system in order for you to use at
job scheduling.
Note
|
In Linux, the |
The most important options which apply to the at
command are:
-c
-
Print the commands of a specific job ID to the standard output.
-d
-
Delete jobs based on their job ID. It is an alias for
atrm
. -f
-
Read the job from a file instead of the standard input.
-l
-
List the pending jobs of the user. If the user is root, all jobs of all users are listed. It is an alias for
atq
. -m
-
Send mail to the user at the end of the job even if there was no output.
-q
-
Specify a queue in the form of a single letter from
a
toz
and fromA
toZ
(by defaulta
forat
andb
forbatch
). Jobs in the queues with the highest letters are executed with increased niceness. Jobs submitted to a queue with a capital letter are treated asbatch
jobs. -v
-
Show the time at which the job will run before reading the job.
List Scheduled Jobs with atq
Now let us schedule two more at
jobs: the first executes the foo.sh
script at 09:30 am, while the second executes the bar.sh
script after one hour.
$ at 09:30 AM warning: commands will be executed using /bin/sh at> ./foo.sh at> Ctrl+D job 13 at Sat Sep 14 09:30:00 2019 $ at now +2 hours warning: commands will be executed using /bin/sh at> ./bar.sh at> Ctrl+D job 14 at Sat Sep 14 11:10:00 2019
To list your pending jobs, you can use the atq
command which shows the following information for each job: job ID, job execution date, job execution time, queue, and username.
$ atq 14 Sat Sep 14 11:10:00 2019 a frank 13 Sat Sep 14 09:30:00 2019 a frank 12 Sat Sep 14 09:15:00 2019 a frank
Remember that the at -l
command is an alias for atq
.
Note
|
If you run |
Delete Jobs with atrm
If you want to delete an at
job, you can use the atrm
command followed by the job ID. For example, to delete the job with ID 14, you can run the following:
$ atrm 14
You can delete multiple jobs with atrm
by specifying multiple IDs separated by spaces. Remember that the at -d
command is an alias for atrm
.
Note
|
If you run |
Configure Access to Job Scheduling
Authorization for ordinary users to schedule at
jobs is determined by the /etc/at.allow
and /etc/at.deny
files. If /etc/at.allow
exists, only non-root users listed within it can schedule at
jobs. If /etc/at.allow
does not exist but /etc/at.deny
exists, only non-root users listed within it cannot schedule at
jobs (in this case an empty /etc/at.deny
file means that each user is allowed to schedule at
jobs). If neither of these files exist, the user’s access to at
job scheduling depends on the distribution used.
Time Specifications
You can specify when to execute a particular at
job using the form HH:MM
, optionally followed by AM or PM in case of 12-hour format. If the specified time has already passed, the next day is assumed. If you want to schedule a particular date on which the job will run, you must add the date information after the time using one of the following forms: month-name day-of-month
, month-name day-of-month year
, MMDDYY
, MM/DD/YY
, DD.MM.YY
and YYYY-MM-DD
).
The following keywords are also accepted: midnight
, noon
, teatime
(4 pm) and now
followed by a plus sign (+
) and a time period (minutes, hours, days and weeks). Finally, you can tell at
to run the job today or tomorrow by suffixing the time with the words today
or tomorrow
. For example, you can use at 07:15 AM Jan 01
to execute a job at 07:15 am on 01 January and at now +5 minutes
to execute a job five minutes from now. You can read the timespec
file under the /usr/share
tree for more information about the exact definition of time specifications.
An Alternative to at
Using systemd as the system and service manager, you can also schedule one-time tasks with the systemd-run
command. It is typically used to create a transient timer unit so that a command will be executed at a specific time without the need to create a service file. For example, acting as root, you can run the date
command at 11:30 AM on 2019/10/06 using the following:
# systemd-run --on-calendar='2019-10-06 11:30' date
If you want to run the foo.sh
script, located in your current working directory, after two minutes you can use:
# systemd-run --on-active="2m" ./foo.sh
Consult the manual pages to learn all possible uses of systemd-run
with systemd-run(1)
.
Note
|
Remember that timers are logged to the systemd journal and you can review the logs of the different units using the |
Guided Exercises
-
For each of the following time specifications, indicate which is valid and which is invalid for
at
:at 08:30 AM next week
at midday
at 01-01-2020 07:30 PM
at 21:50 01.01.20
at now +4 days
at 10:15 PM 31/03/2021
at tomorrow 08:30 AM
-
Once you have scheduled a job with
at
, how can you review its commands? -
Which commands can you use to review your pending
at
jobs? Which commands would you use to delete them? -
With systemd, which command is used as an alternative to
at
?
Explorational Exercises
-
Create an
at
job that runs thefoo.sh
script, located in your home directory, at 10:30 am on coming October 31st. Assume you are acting as an ordinary user. -
Login to the system as another ordinary user and create another
at
job that runs thebar.sh
script tomorrow at 10:00 am. Assume the script is located in the user’s home directory. -
Login to the system as another ordinary user and create another
at
job that runs thefoobar.sh
script just after 30 minutes. Assume the script is located in the user’s home directory. -
Now as root, run the
atq
command to review the scheduledat
jobs of all users. What happens if an ordinary user executes this command? -
As root, delete all these pending
at
jobs using a single command. -
Run the
ls -l /usr/bin/at
command and examine its permissions.
Summary
In this lesson, you learned:
-
Use
at
to run one-time jobs at a specific time. -
Manage
at
jobs. -
Configure user access to
at
job scheduling. -
Use
systemd-run
as an alternative toat
.
The following commands and files were discussed in this lesson:
at
-
Execute commands at a specified time.
atq
-
List the user’s pending
at
jobs, unless the user is the superuser. atrm
-
Delete
at
jobs, identified by their job number. /etc/at.allow
and/etc/at.deny
-
Particular files used to set
at
restrictions. systemd-run
-
Create and start a transient
timer
unit as an alternative toat
for one-time scheduling.
Answers to Guided Exercises
-
For each of the following time specifications, indicate which is valid and which is invalid for
at
:at 08:30 AM next week
Valid
at midday
Invalid
at 01-01-2020 07:30 PM
Invalid
at 21:50 01.01.20
Valid
at now +4 days
Valid
at 10:15 PM 31/03/2021
Invalid
at tomorrow 08:30 AM monotonic
Invalid
-
Once you have scheduled a job with
at
, how can you review its commands?You can use the
at -c
command followed by the ID of the job whose commands you want to review. Note that the output also contains most of the environment that was active at the time the job was scheduled. Remember that root can review the jobs of all users. -
Which commands can you use to review your pending
at
jobs? Which commands would you use to delete them?You can use the
at -l
command to review your pending jobs, and you can use theat -d
command to delete your jobs.at -l
is an alias foratq
andat -d
is an alias foratrm
. Remember that root can list and delete the jobs of all users. -
With systemd, which command is used as an alternative to
at
?The
systemd-run
command can be used as an alternative toat
to schedule one-time jobs. For example, you can use it to run commands at a specific time, defining a calendar timer or a monotonic timer relative to different starting points.
Answers to Explorational Exercises
-
Create
an
at job that runs thefoo.sh
script, located in your home directory, at 10:30 am on coming October 31st. Assume you are acting as an ordinary user.$ at 10:30 AM October 31 warning: commands will be executed using /bin/sh at> ./foo.sh at> Ctrl+D job 50 at Thu Oct 31 10:30:00 2019
-
Login to the system as another ordinary user and create another
at
job that runs thebar.sh
script tomorrow at 10:00 am. Assume the script is located in the user’s home directory.$ at 10:00 AM tomorrow warning: commands will be executed using /bin/sh at> ./bar.sh at> Ctrl+D job 51 at Sun Oct 6 10:00:00 2019
-
Login to the system as another ordinary user and create another
at
job that runs thefoobar.sh
script just after 30 minutes. Assume the script is located in the user’s home directory.$ at now +30 minutes warning: commands will be executed using /bin/sh at> ./foobar.sh at> Ctrl+D job 52 at Sat Oct 5 10:19:00 2019
-
Now as root, run the
atq
command to review the scheduledat
jobs of all users. What happens if an ordinary user executes this command?# atq 52 Sat Oct 5 10:19:00 2019 a dave 50 Thu Oct 31 10:30:00 2019 a frank 51 Sun Oct 6 10:00:00 2019 a emma
If you run the
atq
command as root, all pendingat
jobs of all users are listed. If you run it as an ordinary user, only your own pendingat
jobs are listed. -
As root, delete all of these pending
at
jobs using a single command.# atrm 50 51 52
-
As root, run the
ls -l /usr/bin/at
command and examine its permissions.# ls -l /usr/bin/at -rwsr-sr-x 1 daemon daemon 43762 Dec 1 2015 /usr/bin/at
In this distribution, the
at
command has both the SUID (thes
character instead of the executable flag for the owner) and SGID (thes
character instead of the executable flag for the group) bits set, which means that it is executed with the privileges of the owner and group of the file (daemon
for both). This is why ordinary users are able to schedule jobs withat
.