How to Schedule Future Processes in Linux Using at
Tags: linux, codenewbie, beginners, tutorial
Published: 2022-06-04 13:54:42

This article was originally posted on [Everything DevOps](https://everythingdevops.dev/how-to-schedule-future-processes-in-linux-using-at/).

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)](https://releases.ubuntu.com/18.04/) 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:

```bash
#!/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:

```bash
$ chmod +x testat.sh
```
After making it executable, with the `at`, queue the shell script up to execute precisely 2 minutes from now using:

```bash
$ 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. 

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652968304411_Screenshot+from+2022-05-19+14-51-26.png)


After installing `at`, rerun the above command. 

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652969313595_Screenshot+from+2022-05-19+15-08-03.png)


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:

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652969109534_Screenshot+from+2022-05-19+15-04-53.png)


And you can run `$ atq` to see that the job is queued to run:

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652969517427_Screenshot+from+2022-05-19+15-11-19.png)


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. 

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652969707539_Screenshot+from+2022-05-19+15-14-24.png)


To test if the date was written to the `testingat.txt` file, run:

```bash
$ cat testingat.txt
```
The above command will print out the `date` on which it was run as specified in the shell script:

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652969761632_Screenshot+from+2022-05-19+15-15-44.png)

## 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`.

```bash
$ 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.

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652971955379_annotely_image.png)


In the interactive shell, write the command below and press **Enter.** 

```bash
$ 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. 

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652972300077_Screenshot+from+2022-05-19+15-58-05.png)


Then log out of the shell with `CTRL + D`, and after 2 minutes, you will see the `testingat2.text` has been created.

![](https://paper-attachments.dropbox.com/s_81F2080D5FDA23715EEE67A72CF74FBC9069711FC147A7120EA7900976339C3B_1652972509629_annotely_image+1.png)

## 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       |

## 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:

- [Linux at, batch, atq, and atrm commands](https://www.computerhope.com/unix/uat.htm)
- [Scheduling Tasks in Linux - The AT Command](https://www.youtube.com/watch?v=1xZomRM79xE)
- [at (command) - on Wikipedia](https://en.wikipedia.org/wiki/At_(command))