Computer >> Computer tutorials >  >> System >> Linux

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

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:

sudo apt install at

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

On Fedora, run:

sudo dnf install at

To install “at” on Arch Linux:

sudo pacman -S at

And, on openSUSE:

sudo zypper install at

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:

systemctl is-enabled atd.service

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

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

sudo systemctl enable atd.service

And start the daemon:

sudo systemctl start atd.service

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.

at now + 10 minutes
at now + 10 hours
at now + 10 days
at now + 10 weeks

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

2. Run at exact time:

at 23:10

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

at 11:00

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

3. Run at exact time and date:

at 12:00 December 31

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:

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

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:

sudo -i

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:

exit

This will log you out from the root user account.

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

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:

atq

or

at -l

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

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

at -c 22

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

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

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

atrm 22

Schedule “at” to Run Graphical Applications

First, find out your current display variable value:

echo $DISPLAY

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.

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

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

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

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.