Linux Professional Institute Learning Logo.
Skip to main content
  • Home
    • All Resources
    • LPI Learning Materials
    • Become a Contributor
    • Publishing Partners
    • Become a Publishing Partner
    • About
    • FAQ
    • Contributors
    • Roadmap
    • Contact
  • LPI.org
107.2 Lesson 2
Topic 105: Shells and Shell Scripting
105.1 Customize and use the shell environment
  • 105.1 Lesson 1
  • 105.1 Lesson 2
  • 105.1 Lesson 3
105.2 Customize or write simple scripts
  • 105.2 Lesson 1
  • 105.2 Lesson 2
Topic 106: User Interfaces and Desktops
106.1 Install and configure X11
  • 106.1 Lesson 1
106.2 Graphical Desktops
  • 106.2 Lesson 1
106.3 Accessibility
  • 106.3 Lesson 1
Topic 107: Administrative Tasks
107.1 Manage user and group accounts and related system files
  • 107.1 Lesson 1
  • 107.1 Lesson 2
107.2 Automate system administration tasks by scheduling jobs
  • 107.2 Lesson 1
  • 107.2 Lesson 2
107.3 Localisation and internationalisation
  • 107.3 Lesson 1
Topic 108: Essential System Services
108.1 Maintain system time
  • 108.1 Lesson 1
  • 108.1 Lesson 2
108.2 System logging
  • 108.2 Lesson 1
  • 108.2 Lesson 2
108.3 Mail Transfer Agent (MTA) basics
  • 108.3 Lesson 1
108.4 Manage printers and printing
  • 108.4 Lesson 1
Topic 109: Networking Fundamentals
109.1 Fundamentals of internet protocols
  • 109.1 Lesson 1
  • 109.1 Lesson 2
109.2 Persistent network configuration
  • 109.2 Lesson 1
  • 109.2 Lesson 2
109.3 Basic network troubleshooting
  • 109.3 Lesson 1
  • 109.3 Lesson 2
109.4 Configure client side DNS
  • 109.4 Lesson 1
Topic 110: Security
110.1 Perform security administration tasks
  • 110.1 Lesson 1
110.2 Setup host security
  • 110.2 Lesson 1
110.3 Securing data with encryption
  • 110.3 Lesson 1
  • 110.3 Lesson 2
How to get certified
  1. Topic 107: Administrative Tasks
  2. 107.2 Automate system administration tasks by scheduling jobs
  3. 107.2 Lesson 2

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 batch command is similar to at, however batch jobs are executed only when the system load is low enough to allow it.

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 to z and from A to Z (by default a for at and b for batch). Jobs in the queues with the highest letters are executed with increased niceness. Jobs submitted to a queue with a capital letter are treated as batch 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 atq as root, it will display the queued jobs for all users.

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 atrm as root you can delete the jobs of all users.

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 journalctl command. Also remember that if you are acting as an ordinary user, you need to use the --user option of the systemd-run and journalctl commands.

Guided Exercises

  1. 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

  2. Once you have scheduled a job with at, how can you review its commands?

  3. Which commands can you use to review your pending at jobs? Which commands would you use to delete them?

  4. With systemd, which command is used as an alternative to at?

Explorational Exercises

  1. Create an at job that runs the foo.sh script, located in your home directory, at 10:30 am on coming October 31st. Assume you are acting as an ordinary user.

  2. Login to the system as another ordinary user and create another at job that runs the bar.sh script tomorrow at 10:00 am. Assume the script is located in the user’s home directory.

  3. Login to the system as another ordinary user and create another at job that runs the foobar.sh script just after 30 minutes. Assume the script is located in the user’s home directory.

  4. Now as root, run the atq command to review the scheduled at jobs of all users. What happens if an ordinary user executes this command?

  5. As root, delete all these pending at jobs using a single command.

  6. 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 to at.

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 to at for one-time scheduling.

Answers to Guided Exercises

  1. 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

  2. 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.

  3. 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 the at -d command to delete your jobs. at -l is an alias for atq and at -d is an alias for atrm. Remember that root can list and delete the jobs of all users.

  4. With systemd, which command is used as an alternative to at?

    The systemd-run command can be used as an alternative to at 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

  1. Create an at job that runs the foo.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
  2. Login to the system as another ordinary user and create another at job that runs the bar.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
  3. Login to the system as another ordinary user and create another at job that runs the foobar.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
  4. Now as root, run the atq command to review the scheduled at 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 pending at jobs of all users are listed. If you run it as an ordinary user, only your own pending at jobs are listed.

  5. As root, delete all of these pending at jobs using a single command.

    # atrm 50 51 52
  6. 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 (the s character instead of the executable flag for the owner) and SGID (the s 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 with at.

Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Next Lesson

107.3 Localisation and internationalisation (107.3 Lesson 1)

Read next lesson

Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

LPI is a non-profit organization.

© 2022 Linux Professional Institute (LPI) is the global certification standard and career support organization for open source professionals. With more than 200,000 certification holders, it's the world’s first and largest vendor-neutral Linux and open source certification body. LPI has certified professionals in over 180 countries, delivers exams in multiple languages, and has hundreds of training partners.

Our purpose is to enable economic and creative opportunities for everybody by making open source knowledge and skills certification universally accessible.

  • LinkedIn
  • flogo-RGB-HEX-Blk-58 Facebook
  • Twitter
  • Contact Us
  • Privacy and Cookie Policy

Spot a mistake or want to help improve this page? Please let us know.

© 1999–2022 The Linux Professional Institute Inc. All rights reserved.