The β€œat” Command in Linux [7 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The at command in Linux is a command line utility tool used for scheduling one-time jobs. You can specify a time for executing certain tasks. Moreover, the command can also list or delete jobs that are saved for later execution. To use this command user must have the system permissions. The atq, atrm, and batch commands are also part of the at command that enables it to queue, examine, or remove scheduled jobs.

In this article, I will present you with the possible uses of the at command in Linux with practical examples.

Syntax of “at” Command

The syntax for the at command is:

at [OPTION]... runtime

Note: In the above syntax OPTION enclosed by square brackets and followed by 3 dots represents that multiple options can be utilized simultaneously.

Options for “at” Command

You can find the details of at command’s option on the man page of the command.

man at
Option Description
-b Runs commands when system load levels permit, an alias for batch.
-c Displays specified job context on the terminal.
-f Reads jobs from a file.
-r or -d Removes pending job specified by job ID, an alias for atrm.
-l Lists current users’ pending jobs, an alias for atq.
-m Mails user upon completion of the job.
-M Does not mail the user upon completion of the job.

Note: The options in Linux CLI (Command Line Interface) are all case-sensitive. So be cautious while using them.

Installing β€œat” Command in Linux

The at command may or may not be installed in your system. You can install the at package by following command:

sudo apt-get install at

Installing at command in linux.Now, to enable the atd service on your system, write the following command:

sudo systemctl enable --now atd

Enabling atd service on system.

Note: The atd service is the scheduling daemon of the at command. It allows the at command to execute jobs at a later time

Practical Examples of the β€œat” Command in Linux

With the at command in Linux you will be able to schedule/remove commands for later execution. Moreover, You can use this command in many ways utilizing the associated options. Here are some of the most common uses of the at command in Linux:

1. Scheduling a Job to Run at a Certain Time Using the β€œat” Command

You can specify the time for executing a job using the at command in Linux. It will read your given commands from standard input. For example, to schedule the job β€œtouch file1.txt” (Creating an empty file) to execute later on the same day at 5:00 AM, follow the steps:

  1. Open the terminal and run the below command:
    at 05:00
  2. Now, write the following command as the job:
    touch file1.txt
  3. Finally, press CTRL+D keys to complete the scheduling.

In this given image, you can see that I have scheduled the desired job to execute at 5:00 AM.Scheduling a command using at command in linux.

2. Piping a Job to the β€œat” Command to Run at a Certain Time

You can pipe a command for later execution to the at command in Linux. For example, to echo a text at a specific time, use the command below:

echo "This is a Scheduled echo" | at 05:00 am

In the following image, you can see that I have saved the command as a job for later execution using piping along with the at command.Piping a command to schedule using at command in linux.


Similar Readings


3. Listing Pending Jobs Using the β€œat” Command

To list all the pending jobs, use either the atq command or the -l option along with the at command. For example, type any of the following commands in the command prompt:

atq

Or,

at -l

You can see that all the pending jobs are listed using the atq command.Listing pending jobs using atq command in linux.Here, all the pending jobs are listed using the at -l command:
Listing pending jobs using at command in linux.

4. Viewing a Specified Job Contents Using the β€œat” Command

To view a specific job content, use option -c and a specific job ID. For example, to show the contents of the job having ID 5, run the command:

at -c 5

In the image, you can see thatΒ  the contents of the job with ID 5.Viewing job contents using at command in linux.

5. Reading a Job From a File Using the β€œat” Command

To read a job from a file, use the -f option with at command. For example, you have a file named β€œjobs.txt” to execute commands at this instant. To do the that use the below command:

at -f jobs.txt now

In the image below, you can see that the β€œjobs.txt” file contains commands for creating folder1 and folder2. These commands are executed using the at command. You can use the ls command to see the newly created folders.Reading job from a file using at command in linux.


Similar Readings


6. Scheduling a Job to Execute When the System Load Levels Permit

By default, option -b permits the execution of commands when the average load is below 1.5. That is, one process is running while another one is waiting for 50% of the time. This option is an alias for the batch command. Now, let’s say, you want to schedule the job β€œtouch file5” (Creating empty files) to execute when the average load is below 1.5. To do so, follow the steps below.

  1. Open the terminal and type any of the following commands:
    at -b

    Or,

    batch
  2. Press the ENTER button.
  3. Now, write the following command as the job:
    touch file5
  4. Finally, press CTRL+D keys to complete scheduling.

In the following image, you can see that the desired command is scheduled to execute when the system load level permits.Scheduling a job when system load permits using at command in linux.

7. Removing a Specific Job Using the β€œat” Command

To remove a specific job from the scheduled list, use the job ID along with the options -r or -d. Moreover, using the command atrm with a specific job ID will also give you the same result. For example, to remove the job with ID 3 using the atrm command, use any of the following commands:

atrm 3

Or,

at -r 3

Or,

at -d 3

In the image below, you can see Job 3 is removed.Removing a pending job using at command in linux.

Time Specification of “at” Command

The time structure of at command is “Day Month Date hh:mm:ss Year”. The time specifications and their relative translation of at command are below:

Time Specifications Translated Time
now Tue Jan 17 07:00:00 2023
noon Tue Jan 17 12:00:00 2023
teatime Tue Jan 17 16:00:00 2023
midnight Wed Jan 18 00:00:00 2023
Tomorrow Or, now + 1 day Wed Jan 18 07:00:00 2023
Fri Fri Jan 20 07:05:00 2023
next week Or, now + 7days Or, next Tue Tue Jan 24 07:00:00 2023
next month Or, now + 1 month Fri Feb 17 07:00:00 2023
08:00 Or, 08:00 AM Or, now + 1hour Tue Jan 17 08:00:00 2023
08:00 PM Tue Jan 17 20:00:00 2023
2:30 PM Feb 23 Or, 2:30 PM 23.02.23 Thu Feb 23 14:30:00 2023
now + 30 minutes Tue Jan 17 07:30:00 2023
now + 1 year Wed Jan 17 07:00:00 2024

Conclusion

In this article, I have tried to show you some frequent uses of the at command in Linux. The options available under this at command work as alternatives to a few other commands. That is why the commands atq, atrm, and batch are considered as a part of the at command. I hope these practical examples of the at command described here, will help you with the command line and make you a power user of Linux.

People Also Ask

How to automate a process in Linux?

The at command in Linux is a useful tool that allows you to schedule commands to be executed at a specific time in the future. To create a new job with the at command, you need to enter the time and date when you want the command to run, followed by a space and then the command itself. You can also pipe the command from another program or file using echo or Here document. For example:

echo "date" | at 10:00

This will create a job that will run the date command at 10:00 AM.

What is the difference between at and batch command?

The at command is great for scheduling one-time tasks that need to run at a particular time, while batch is suitable for running tasks when the system has free resources to ensure minimal impact on the system’s performance. Some key differences:

at Command:

  1. It schedules a command or a set of commands to run once at a specific time.
  2. You can specify the exact date and time for the execution of the task.
  3. The output of the command is mailed to the user after execution.

batch Command:

  1. It schedules tasks to be executed when the system load average drops below a certain level (usually 1.5).
  2. Unlike at, it does not run at a specific time but waits until the system is less busy.
  3. You can only use the batch command interactively, adding commands to the queue.

How do I know if at is installed in Linux?

To check if the at command is installed on your Debian/Ubuntu systems, use:

sudo apt list --installed | grep at

For CentOS/Fedora systems, use:

sudo yum list installed | grep at

How do I install the at utility in Linux?

To install the at utility package on your Debian/Ubuntu systems, use:

sudo apt update && sudo apt install -y at

For CentOS/Fedora systems, use:

sudo yum install at

After installing, you should ensure that the atd service, which is the scheduling daemon for at, is running and enabled to start on boot by:

sudo systemctl enable --now atd

Similar Readings

1.5/5 - (6 votes)
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment