at command in Linux with examples

In Linux, you can automate many processes using various commands. You can even automate the process of running those commands. You can use the at command to run commands at a specified point in the future or it will only run these commands once. You can specify the time using key words like “tomorrow,” next week,” etc.

You might be thinking that at sounds similar to cron – the scheduler built into Linux systems. The big difference between at and cron is that at will only run one time, whereas cron is used to run things repeatedly, on a certain schedule.

In this tutorial, we will discuss the common applications of at in Linux and some of its options to help you best utilize command scheduling. On certain Linux distributions, this command may not be installed by default. In this case, you’ll need to install it manually. We’ll include instructions for that below.

In this tutorial you will learn:

  • How to install at on major Linux distros
  • How to use at on Linux
at command in Linux with examples
at command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software at
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install at in Linux




In case at is not already installed on your system by default, you can use the appropriate command below to install it with your system’s package manager.

To install at on Ubuntu, Debian, and Linux Mint:

$ sudo apt install at

To install at on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install at

To install at on Arch Linux and Manjaro:

$ sudo pacman -S at

Frequently Used Options

Once you are done with the installation, you can begin using it to schedule jobs to be done in the future.

at command in Linux Basic Examples



  1. As a basic example, let’s use the rm command to remove a file or a directory at a point in time such as tomorrow. But if you’re a newcomer to Linux, you might be pretty confused as to how at works, as it’s not run using the typical syntax of many basic Linux commands. For at, you must first type the at command itself and then the specified time after it. You’ll then hit Enter and type the desired commands in the prompt you’re presented with below. at will also warn about which shell the commands will run in.
    $ at tomorrow
    at> rm file01
    

    Once you’ve typed your commands, you can either cancel out and stop the at prompt with Ctrl + Z or save and confirm the job with Ctrl + D.

    Using the at command and the rm command to remove file01 tomorrow
    Using the at command and the rm command to remove file01 tomorrow

    As you can see in the screenshot above, at will remove file01 at exactly this time tomorrow with the rm command.

  2. If you’re wondering what to do if you forget the jobs that you’ve scheduled to be executed at many different times, don’t worry. You can view all the jobs freely in a list using the -l option with at. Alternatively, you can simply run the atq command to achieve the same effect.
    $ at -l
    

    Using the -l option with the at command to view a list of all the jobs we’ve scheduled
    Using the -l option with the at command to view a list of all the jobs we’ve scheduled
  3. As you may have noticed, utilizing expressions of time in the syntax of at such as tomorrow or tuesday, by default, the jobs will be scheduled on those days at the current time. You can specify the time further in a variety of ways. In this example, we add an extra 45 minutes onto the current timestamp.
    $ at tomorrow +45 minutes
    at> echo “hello”
    

    Using the at command to schedule a command for tomorrow at a time that is 45 minutes later than the current time
    Using the at command to schedule a command for tomorrow at a time that is 45 minutes later than the current time
  4. We can also specify the exact time directly for the jobs to be executed utilizing at.


    $at tomorrow 3:30
    at>echo “hello”
    

    Schedule a job to be executed at a specific time
    Schedule a job to be executed at a specific time
  5. You can also specify the time that the jobs will be executed in a way opposite to one of the previous examples. Instead of what we did above, where we specified the day and added extra time from our current time, we can determine the exact time and add an additional number of days from now.
    $ at 3:45 +2 days
    at>echo “hello”
    

    Using the at command to schedule a job to be executed at a specific time two days from now
    Using the at command to schedule a job to be executed at a specific time two days from now
  6. As you can see, the time specifications that we can apply can get decently complex to meet your needs in various situations. But we’ve yet to cover the most straightforward application; we can schedule a job to be executed minutes or hours from the current time. The syntax for this is a little counter-intuitive if you’re not entirely familiar with at. You will need to specify now and then add the minutes or hours to after it.
    $ at now +15 minutes 
    at>echo “hello”
    

    Schedule a job to be executed 15 minutes from now
    Schedule a job to be executed 15 minutes from now
  7. To remove jobs that you’ve scheduled with at, you can utilize the -r option, or the atrm command to achieve the same effect. In order to successfully remove a job utilizing this command, you’re required to specify the job ID. As we’ve covered in a previous example, at can be used with the -l option to display a list of every job you’ve scheduled. Along with the ETA of the jobs, the job ID is included in the list. For example: 13 Thu Sep 9 01:00:00 2021. The job ID in this example is 13.
    $ at -r  13
    

    Execution with the -r option to remove the job with a job ID of 13
    Execution with the -r option to remove the job with a job ID of 13
NOTE
You can always use the man command to read more about the at command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

at is pretty simple, but as you’ve observed throughout the examples section of this article, it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know them. However, they can definitely come in handy in various situations. In this section of the tutorial, we’ll show you a few of the lesser known options of at that we think are useful.

at command in Linux Advanced Examples

  1. If you have configured an email address for your user account and your system is setup with the ability to send emails, at will send email notifications to the user by default, upon successful exection of a scheduled command. If you wish to avoid this process, you can force at to hold off the notification using the -M option. Please note that these command line options are case sensitive, and if you type a lowercase -m, you will not get the desired function.
    $ at -M 
    at>echo “hello”
    
  2. We can also pass the -m command line option to at to receive an email notification for a job that is executed even if it does not produce an output.
    $ at -m
    at>echo “hello”
    


Closing Thoughts

In this tutorial, we learned all about at in Linux. at is essential to master for users and administrators that frequently need to schedule commands to be executed in the future. For recurring command execution, you can always resort to cron. Otherwise, for single use, at is the perfect tool for the job.