Linux
How to Schedule Future Processes in Linux Using at
Teach you how to schedule future processes using the at command.
Divine Odazie
May 20, 2022
Published on
May 20, 2022
Read More
Divine Odazie
19 Jan, 2025
Teach you how to schedule future processes using the at command.

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 use at interactively, and
  • How to specify time and date when using at.

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 be 1 if you’re using at 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:

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:

Stay ahead with the latest updates, exclusive insights, and tailored solutions by joining our newsletter.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
What is your email addres?
Featured posts
Linux background and foreground process management
Learn how to manage Linux terminal processes
This is some text inside of a div block.
This is some text inside of a div block.

Stay ahead with the latest updates, exclusive insights, and tailored solutions by joining our newsletter.

We care about your data in our privacy policy.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
ABOUT THE AUTHOR