How to Schedule Commands in Linux with the “at” Utility

The first time I managed a busy server, I had to wait for an opportune time to reboot it when people weren’t using it. I noticed that this happened around 3AM. Then, I thought to myself: “Well this is inconvenient, I have to wake up at an unreasonable hour just to reboot my server.” Five seconds later I realized that was a stupid thought. “This is Linux, I’m sure it’s easy to automate such a simple task.” And so, at was discovered.

The utility is very easy to use. You just pass the time and date as a command line parameter, and then enter one or more commands you want it to execute.

On a server it’s useful to have such a program because you often want it to take actions, unattended. But even on a desktop at can be a very convenient tool. For example, say you download a very large file. You can schedule your PC to shut down late into the night. Also, you can tell at to remind you about things you need to do. Pretty much any action you can think of can be scheduled with the right tools and command line options.

How to Install “at” on Various Linux Distributions

On Debian or distributions based on Debian, run:

linux-at-install-on-debian

On Fedora, run:

To install “at” on Arch Linux:

And, on openSUSE:

Enable Automatic Startup of “at” Daemon and Run Service

Some distributions will automatically enable the startup of the “at” daemon at boot. Others won’t. Check with:

linux-at-is-enabled-atd

If it says “disabled,” then enable it with:

And start the daemon:

How to Specify Date and Time to Schedule “at” Commands

You can use any one of the following forms.

1. Run command after specified number of minutes, hours, days or weeks.

linux-at-now-plus-6-days

2. Run at exact time:

If it’s already 12:00, and you run:

Then the command will run tomorrow, at the specified time.

3. Run at exact time and date:

Other alternative ways to specify time and date can be found in the online “at” manual.

How to Use the “at” Command

As you may have noticed, after you specify the scheduled time, you’re dropped at a prompt that looks like the following image:

linux-at-prompt

Here, you simply enter commands that you want to run. These will execute under your current username. Type the command you want to run at a specified time and press Enter. If you want to run a subsequent command, repeat the same procedure. When you’re done, press Ctrl + D. <EOT> will be displayed when you press those keys, followed by the time when the command(s) will be executed.

If you want to run commands that require root privileges, don’t use sudo. Remember, the command will run unattended, so sudo won’t work because no one can enter the password. Instead, first log in as the root user:

And then use the “at” command normally. Now, all commands will be executed as root, instead of your regular user.

After you schedule your command, type:

This will log you out from the root user account.

linux-at-schedule-poweroff

In the example offered in this screenshot, a shutdown was scheduled in two minutes. You can adapt this for your own purposes.

If the computer is powered off before a scheduled job has a chance to run, it will simply run at the next boot (if its time has passed). For example, if you schedule a job at 3PM, shut down at 2PM, and power on at 4PM, the job will run at 4PM.

View and/or Remove Scheduled Jobs

You can view queued jobs with:

or

linux-at-view-queue

To see what commands are scheduled in a job, use the prefix number of that job.

linux-at-cat-job

The last lines of output will show you the commands you scheduled.

To remove a job, use its prefix number like this:

Schedule “at” to Run Graphical Applications

First, find out your current display variable value:

Usually, it will be :0.

Schedule at to run. In the “at” prompt, set the DISPLAY variable to the previous value (export DISPLAY=:0), and run your desired graphical application.

linux-at-graphical-app

The commands used in the previous screenshot would result in the following.

linux-at-libreoffice-scheduled-open

Now the graphical app will be “in your face” and remind you of an important job better than a phone alarm would.

Conclusion

Using “at” is easy. The harder part is finding out a creative way to take an action you desire. But you can pretty much do anything you want, like start a download, close a program, put your computer in standby, and so on.

2 comments

  1. It’s wonderful that you showed how to install ‘at’ on different distributions but the you dropped the ball and assumed that EVERYONE is using systemd. There still are many distros that do not use it as the init program. How about showing your readers how to enable automatic startup of “at” daemon and run service for init programs other than systemd?

  2. The ‘at’ command seems very useful. I wasn’t even aware it existed. Thanks for covering all the main options! The article makes for a great quick-start manual.

    Just tested it on my system (debian-based) using the commands listed here and it was not only installed, the deamon was also running (systemctl is-enabled atd.service).

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.

Sponsored Stories