How to Schedule One-Time Jobs on Linux Using at


Time management is a difficult art to master. Fortunately, with the help of technology, you can automate and delegate mundane tasks to your computer. Unlike humans, PCs are very good at running repetitive tasks at a precise set time.

On Linux, you can run repetitive tasks using tools such as cron. In addition, you can also schedule and run one-time tasks using the at command.

Installing at on Linux

The at command is not part of the standard Linux utilities on most distros. Luckily, you can easily install it using your package manager regardless of the distro you’re using.

On Debian-based systems such as Ubuntu, MX Linux, and Pop!_OS, you can install at by running:

 sudo apt install at 

If you are using an RPM-based distro such as RHEL, Fedora, or Rocky Linux, use DNF to install the package:

 sudo dnf install at 

On Arch-based Linux distros like Manjaro, run:

 sudo pacman -S at 

Starting the atd Job Scheduler Service

Before using the at command, make sure that the atd scheduling daemon is running. This is what the at command uses to execute scheduled jobs.

 sudo systemctl status atd 
checking the status of the atd time scheduler

If the atd service is not running, you can start it using:

 sudo systemctl start atd 

Scheduling Tasks on Linux With at

Here’s how you can schedule a one-time job to be executed at some given time in the future using at:

 command | at time_stamp 

For example, we can schedule a task to list the contents of your current directory using the ls command and write the output to a file a minute from now.

First, change to your user home folder using the cd command:

 cd ~ 

Then, execute the following command to schedule your task:

 ls > list_items.txt | at now + 1 minutes 
scheduling a simple job with at

The output will state that your task has been scheduled. After the time has elapsed, you can list the contents of your directory and a new text file with the directory contents should be present.

If you want to execute the command at noon tomorrow, run:

 ls > list_items.txt | at noon tomorrow 

For more specific times and dates, use the date format MMDDHHMM YYYY. For example, to execute the preceding command at 1 p.m. on 25th December 2023, you can run the command:

 ls > list_items.txt | at 12251300 2023 

The at command has a lot more advanced features that allow you to run specific commands at a precise time. Take a look at its man pages for more command options:

 man at 

Scheduling Scripts on Linux Using at

Apart from running individual commands, you can also use the at command to schedule scripts to be executed at specified times.

Let’s assume that you have a script named, located in your home folder. The script simply prints out hard disk usage to a text file. Here’s the script:

df -h > disk_usage.txt

To execute this script thirty minutes from now, you can simply run the following command:

 at now + 30 minutes -f ~/ 

Make sure that your script is executable by giving it the appropriate mode. To do that, you can use the chmod command:

 sudo chmod +x 

Managing Pending at Tasks on Linux

You can view pending tasks using the following command:

listing jobs with atq

To delete a pending task, use the following command format:

 atrm task_number 

For example, to delete task number 12, use:

 atrm 12 

Automate Repetitive Tasks on Linux With at

The at command is a powerful and versatile tool for executing or running one-time jobs on your Linux PC. Apart from at, you can use crontab to automate all sorts of tasks, ranging from simple to complex jobs.


Source link

Leave a Reply

%d bloggers like this: