How to schedule Linux commands with 'at'

Utility at very easy to use. Users only need to give time and date as command line parameters, then enter one or more commands to execute.

If you have to manage a 'busy' server, you have to wait for an appropriate time to restart the server (ie when people don't use it). This appropriate time is probably at dawn. But this is inconvenient! Does the administrator have to wake up early just to restart his server? With Linux, automating such a simple task is easy with the 'at' utility .

This utility is very easy to use. Users only need to give time and date as command line parameters, then enter one or more commands to execute.

On a server, it is useful to have such a program because users often want the server to perform unattended actions. But even on desktops, at can also be a very convenient tool. For example, suppose you download a very large file. You can schedule your PC to turn off late at night. In addition, users can request at remind them of what to do. Many actions can be scheduled with the appropriate command line tools and options.

Schedule tasks with utilities at Linux

  1. How to install at different Linux distributions
  2. Enable the auto-start daemon at
  3. How to specify the date and time to schedule at commands
  4. How to use the at command
  5. View and / or delete scheduled jobs
  6. Schedule at to run graphics applications

How to install at different Linux distributions

On Debian or Debian-based distributions, run:

 sudo apt install at 

Picture 1 of How to schedule Linux commands with 'at'

On Fedora, run:

 sudo dnf install at 

To install at on Arch Linux, enter the command:

 sudo pacman -S at 

And, on openSUSE, enter the command:

 sudo zypper install at 

Enable the auto-start daemon at

Some distributions will automatically activate daemon boot at boot time. But some other distributions do not. Please check with:

 systemctl is-enabled atd.service 

Picture 2 of How to schedule Linux commands with 'at'

If the result is 'disabled', then enable it with:

 sudo systemctl enable atd.service 

And start the daemon:

 sudo systemctl start atd.service 

How to specify the date and time to schedule at commands

Users can use one of the following forms.

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

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

Picture 3 of How to schedule Linux commands with 'at'

2. Run at an exact time:

 at 23:10 

If it is 12:00, and you run:

 at 11:00 

The order will run tomorrow, at the time specified.

3. Run at the specified time and date:

 at 12:00 December 31 

Other ways to determine the time and date can be found in the online utility manual. (link reference: https://linux.die.net/man/1/at)

How to use the at command

As you can see, after specifying the scheduled time, a command prompt like the following image will appear:

Picture 4 of How to schedule Linux commands with 'at'

Here, users only need to enter the commands they want to run. They will be executed under the current user name. Enter the command you want to run at a specified time and press Enter. If you want to run the next command, repeat the same process. When done, press Ctrl + D. will be displayed when the keys are pressed, followed by the time the command (s) will be executed.

If you want to run commands that require root access, do not use sudo. Remember, the command will run without supervision, so sudo will not work because no one has entered the password. Instead, first log in as the root user:

 sudo -i 

And then use the at command as usual. Now, all commands will be executed with root privileges, instead of regular users.

After scheduling the order, enter:

 exit 

This command will log you out from the user root account.

Picture 5 of How to schedule Linux commands with 'at'

In the example in this screenshot, the shutdown is scheduled for two minutes. Users can adjust it to suit their own needs.

If the computer is turned off before a scheduled job has a chance to run, that task will run at the next boot (if the time is set for it to be over). For example, if the work schedule is scheduled at 3 pm, turn off the computer at 2 pm and turn on the power at 4 pm, the work will run at 4 pm.

View and / or delete scheduled jobs

Users can view jobs ordered by order:

 atq 

Or command:

 at -l 

Picture 6 of How to schedule Linux commands with 'at'

To see which commands are scheduled on a job, use the prefix number of the job.

 at -c 22 

Picture 7 of How to schedule Linux commands with 'at'

The final output lines will show the user the scheduled commands.

To delete a job, use its prefix number as follows:

 atrm 22 

Schedule at to run graphics applications

First, find the current display variable value:

 echo $DISPLAY 

Usually, it will be : 0.

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

Picture 8 of How to schedule Linux commands with 'at'

The commands used in the previous screenshot will result in the following results.

Picture 9 of How to schedule Linux commands with 'at'

Now, the graphics application will prompt users for an important job better than the phone alarm.

Use at very easy. The harder part is finding a creative way to perform an action you desire. But you can do whatever you want, like starting to download, close the program, put the computer on standby, etc.

Hope you are succesful.

Update 26 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile