The at command in Linux is a Command line utility tool used for scheduling one-time jobs. Users can specify a time for executing certain tasks. The command can also list or delete jobs that are saved for later execution. To use this command user must have the system permissions. In this article, I will present you with the possible uses of the at command in Linux with practical examples.
A. Description
The at command in Linux executes some commands from the standard input or a specified file. The atq, atrm, and batch commands are also part of the at command that enables it to queue, examine or remove scheduled jobs.
- at: Execute commands at the specified time.
- atq: List current users’ pending jobs.
- atrm: Remove pending job specified by job ID.
- batch: Runs commands when system load levels permit. By default, if the load average is below 1.5. And by 1.5 means, the system is 100% busy running 1 process while there is another process in the queue waiting to use 50% of the system.
B. Syntax
The syntax for the at command in Linux is as follows.
at [OPTION]... runtime
C. Options
The at command in Linux comes with several options. These can modify your experience regarding job scheduling. Here, I am describing the offered options for your convenience. You will find more details on the man page of the command.
man at
Useful Options
- -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/-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.
Installing the “at” Command in Linux
The at command may or may not be installed in your system. You can find out whether you have it on your machine or not by typing the command at itself.
at
Your system will print out the following message if you do not have it installed:In this case, don’t be overwhelmed. You can install the at package within a minute by following the instructions below.
Steps to Follow >
➊ At first open the Ubuntu Terminal
➋ Type the following in your command prompt:
sudo apt-get install at
➌ Type your password
➍ Press ENTER
❺ Now, to enable the atd service on your system write the following command:
sudo systemctl enable --now atd
❻ Again, Hit ENTER.
Output >
Upon completion of the 1st four steps, at will be installed on your machine.Then, following steps 5 and 6 will let you enable the atd service on your system, so that you can schedule any task or command.
Time Specification for Job Scheduling
The at command in Linux allows a set of time specifications. It accepts absolute time expressions (i.e 08:00 AM, 2:30 PM 23.02.23, etc.), relative time expressions (now, tomorrow, next day, etc.) as well as time units (i.e. 30 minutes, 1 hour, etc.) In this section, you will find the time and date specifications for scheduling a job using the at command in Linux.
Let us assume that, today is Tuesday, 17th January 2023 and the current time is 07:00 AM. Now, the time specifications and their relative translation is described 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 |
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. In this section, you will be able to learn about some of the most common uses of the at command in Linux with hands-on examples.
Example 1: Scheduling a Job to Run at a Certain Time Using the “at” Command in Linux
You can specify the time for executing a job using the at command in Linux. The at command will read your given commands from standard input. In this example, I will show you how to schedule the job “touch file1.txt” (Creating an empty file) to execute later on the same day at 5:00 AM. You can follow the steps below to get the same output.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
at 05:00
➌ Press the ENTER button.
➍ Now, write the following command as the job:
touch file1.txt
❺ Finally, press CTRL+D or ^d keys to complete Scheduling
Output >
In this given image, you can see that I have scheduled the desired job to execute at 5:00 AM.
Example 2: Piping a Job to the “at” Command in Linux to Run at a Certain Time
You can pipe a command for later execution to the at command in Linux. In this example, I will pipe the “echo “This is a Scheduled echo” | at 05:00 am” command to at. The given echo command will execute at 5:00 AM. Follow the given instructions to do the same.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
echo "This is a Scheduled echo" | at 05:00 am
➌ Now, press the ENTER button.
Output >
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.
Similar Readings
- The “man” Command in Linux [6 Practical Examples]
- The “history” Command in Linux [6 Practical Examples]
- The “cal” Command in Linux [7 Practical Examples]
- The “bc” Command in Linux [10+ Practical Examples]
Example 3: Listing Pending Jobs Using the “at” Command in Linux
You can list all the pending jobs using the at command in Linux. To do so you can use either the atq command or the -l option along with the at command. In this example, I will demonstrate the use of both commands to list the pending jobs on my system. You can do the same by following the steps below.
Steps to Follow >
➊ At first launch the Ubuntu Terminal.
➋ Type any of the following commands in the command prompt:
atq
Or,
at -l
➌ Press the ENTER button.
Output >
In the 1st image below, you can see that I have listed all the pending jobs using the atq command.In this 2nd image, you can see that I have listed all the pending jobs using the at command.
Example 4: Viewing a Specified Job Contents Using the “at” Command in Linux
You will be able to view a scheduled job’s contents using the at command in Linux. To get this output you will need to use option -c and a specific job ID. In this example, I will show you the contents of the job having ID 5. Follow the below process to do the same.
Steps to Follow >
➊ Go to the Ubuntu Terminal.
➋ Type the following command in the command line:
at -c 5
➌ Now, hit the ENTER button.
Output >
In the given image, you can see that I have shown the contents of the job with ID 5.
Example 5: Reading a Job From a File Using the “at” Command in Linux
You can read jobs from a specified file instead of reading from the standard input while using the at command. You will need to use the -f option to read commands from a file. In this example, I will use the “jobs.txt” file to execute commands at this instant. To do the same you can follow the steps below.
Steps to Follow >
➊ Launch the Ubuntu Terminal.
➋ Type the following command in the command prompt:
at -f jobs.txt now
➌ Press the ENTER button.
Output >
In the image below, you can see that the “jobs.txt” file contains two 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.
Similar Readings
- The “cron” Command in Linux [3 Practical Examples]
- The “crontab” Command in Linux [10 Practical Examples]
- The “date” Command in Linux [8+ Practical Examples]
- The “ncal” Command in Linux [8 Practical Examples]
Example 6: Scheduling a Job to Execute When the System Load Levels Permit
You can schedule a job to execute when the system load level permits using the at command in Linux. The system load is the number of processes running at any given time. 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. In this example, I will schedule the job “touch file5”(Creating empty files) to execute when the average load is below 1.5. To do so you can follow the given instructions.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type any of the following commands in the command prompt:
at -b
Or,
batch
➌ Press the ENTER button.
➍ Now, write the following command as the job:
touch file5
❺ Finally, press CTRL+D or ^d keys to complete Scheduling
Output >
In the following image, you can see that I have scheduled the desired command to execute when the system load level permits.
Example 7: Removing a Specific Job Using the “at” Command in Linux
You can remove a job from the pending jobs using the at command in Linux. You will need to use the job ID along with the options -r or -d to remove a job. Moreover, using the command atrm with a specific job ID will also give you the same result. The options -r/-d are actually aliases of the atrm command. In this example, I will remove the job with ID 3 using the atrm command. You can do the same by following the steps below.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Type any of the following commands in the command line:
atrm 3
Or,
at -r 3
Or,
at -d 3
➌ Hit the ENTER button.
Output >
In the image below, you can see that I have removed Job 3 using the atrm command.
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.
Similar Readings
- The “neofetch” Command in Linux [12+ Practical Examples]
- The “tree” Command in Linux [14 Practical Examples]
- The “whatis” Command in Linux [12 Practical Examples]
- The “whereis” Command in Linux [10 Practical Examples]
- The “which” Command in Linux [3 Practical Examples]
- The “tee” Command in Linux [4 Practical Examples]