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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
$ sudo apt install at
$ sudo dnf install at
$ 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
- 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
Enterand 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 + Zor save and confirm the job with
Ctrl + D.
As you can see in the screenshot above, at will remove
file01at exactly this time tomorrow with the rm command.
- 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
-loption with at. Alternatively, you can simply run the atq command to achieve the same effect.
$ at -l
- As you may have noticed, utilizing expressions of time in the syntax of at such as
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”
- We can also specify the exact time directly for the jobs to be executed utilizing at.
$at tomorrow 3:30 at>echo “hello”
- 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”
- 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
nowand then add the minutes or hours to after it.
$ at now +15 minutes at>echo “hello”
- To remove jobs that you’ve scheduled with at, you can utilize the
-roption, 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
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.
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
- 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
-Moption. 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”
- We can also pass the
-mcommand 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”
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.