How to Schedule Future Processes in Linux Using at
Table of Contents
Assuming you need to perform a task on a specific day sometime in the future. However, you know you will be away from the machine (computer) on that day. How will you perform the task? You can use the at
terminal utility program to execute any non-interactive command at a specified time.
This article will teach you how to schedule future processes using the at
command. In the end, you would have learned:
- How to schedule commands with
at
using a shell script, - How to schedule commands with
at
interactively, - When to use
at
with a shell script and when to useat
interactively, and - How to specify time and date when using
at
.
Encore is the Development Platform for building event-driven and distributed systems. Move faster with purpose-built local dev tools and DevOps automation for AWS/GCP. Get Started for FREE today.
Prerequisite
To follow this article, you need access to a Linux distribution terminal window. This article uses a Linux Ubuntu 18.04.6 LTS (Bionic Beaver) distribution.
Scheduling a command with at
using a shell script
To schedule a command with at
using a shell script, you will need to first create the script file.
In any directory, create a file testat.sh
in the file, add:
#!/bin/bash
date > testingat.txt
The above shell script, when run, will write the date at that time to a testingat.txt
file that the command will create as it doesn’t exist.
Next, in your terminal, make the shell script file executable with:
$ chmod +x testat.sh
After making it executable, with the at
, queue the shell script up to execute precisely 2 minutes from now using:
$ at now + 2 minute -f testat.sh
Note: Suppose you get an error similar to Command 'at' not found
. It means that the at
utility program isn’t installed on your machine. The output should also prompt you on how to install it, as you can see below for the Ubuntu 18.04.6 LTS distribution used in this article.
After installing at
, rerun the above command.
The above command creates a background job with job number 3
to execute 2 minutes from the time the at
command ran, which was Thu May 19 15:06:00 2022
for this article.
Note: Your job number will be1
if you’re usingat
for the first time.
If you run $ ls
, you will see that it is yet to be executed, and the testingat.txt
file is yet to be created:
And you can run $ atq
to see that the job is queued to run:
Once it’s two minutes past the time you ran the at
command, run $ ls
again, and you will see the testingat.txt
has been created.
To test if the date was written to the testingat.txt
file, run:
$ cat testingat.txt
The above command will print out the date
on which it was run as specified in the shell script:
Interactively scheduling a task with at
Schedule a task with at
interactively is similar to the shell script, but you use the at>
prompt this time.
In your terminal using the same directory, run the command below to execute commands two minutes from now
.
$ at now + 2 minute
The above will then open an interactive shell (annotated below). You will write the commands to be executed 2 minutes after logging out of the shell.
In the interactive shell, write the command below and press Enter.
$ date > testingat2.text
The above command writes the date at that time to a testingat2.text
file that the command will create as it doesn’t exist.
Then log out of the shell with CTRL + D
, and after 2 minutes, you will see the testingat2.text
has been created.
When to use at
with shell script and when to use at
interactively
You should use at
with a shell script:
- If you want to schedule many commands.
- If you're going to share the scheduled commands with someone else
You should use at
interactively:
- If you want to schedule a single command, such as a system update
- If you want to work at a fast pace.
Specifying time and date when using at
.
The at
utility program uses a very casual representation of time and date. at
even knows some "commonly used" times you might not expect — for instance, "teatime" which is traditionally at 4 PM.
Below are some examples of times you can pass to at
to schedule a command. For instance, assuming the current time is 10:00 AM, Thursday, May 19, 2022. The expressions below would translate to the following times:
The expression: | Would translate to: |
---|---|
noon | 12:00 PM May 19 2022 |
midnight | 12:00 AM May 20 2022 |
teatime | 4:00 PM May 19 2022 |
tomorrow | 10:00 AM May 20 2022 |
noon tomorrow | 12:00 PM May 20 2022 |
next week | 10:00 AM May 26 2022 |
next monday | 10:00 AM May 23 2022 |
fri | 10:00 AM May 20 2022 |
NOV | 10:00 AM November 19 2022 |
9:00 AM | 9:00 AM May 20 2022 |
2:30 PM | 2:30 PM May 19 2022 |
1430 | 2:30 PM May 19 2022 |
2:30 PM tomorrow | 2:30 PM May 20 2022 |
2:30 PM next month | 2:30 PM June 19 2022 |
2:30 PM Fri | 2:30 PM June 24 2022 |
2:30 PM 10/21 | 2:30 PM October 21 2022 |
2:30 PM Oct 21 | 2:30 PM October 21 2022 |
2:30 PM 10/21/2022 | 2:30 PM October 21 2022 |
2:30 PM 21.10.22 | 2:30 PM October 21 2022 |
now + 30 minutes | 10:30 AM May 19 2022 |
now + 1 hour | 11:00 AM May 19 2022 |
now + 2 days | 10:00 AM May 19 2022 |
4 PM + 2 days | 4:00 PM May 21 2022 |
now + 3 weeks | 10:00 AM June 9 2022 |
now + 4 months | 10:00 AM September 19 2022 |
now + 5 years | 10:00 AM May 19 2027 |
Encore is the Development Platform for building event-driven and distributed systems. Move faster with purpose-built local dev tools and DevOps automation for AWS/GCP. Get Started for FREE today.
Conclusion
In this article, you learned how to schedule future processes using a shell script and the at>
prompt with the at
command. Also, you learned when to use each option and saw how to represent times in at
.
Resources
There is more to learn about at
. To learn more, check out the following resources:
The Practical DevOps Newsletter
Your weekly source of expert tips, real-world scenarios, and streamlined workflows!