How to Schedule a One-Time Task in Linux Using the at Command

When working in Linux, the cron daemon is famous for scheduling recurring tasks. But what if you only want a command to run exactly once, at a very specific time, and never again? Setting up a cron job for a single occurrence is overly complicated and requires you to manually delete the job afterward.

For one-off scheduled tasks, Linux provides a much simpler tool: the at command. Whether you want to restart a service at midnight, download a large file during off-peak hours, or send an automated email tomorrow morning, the at command handles it flawlessly.

In this guide, you will learn how to install, configure, and use the at command to schedule one-time tasks in the Linux terminal.

Step 1: Install the at Command

While many Linux distributions include at by default, some lightweight installations (like Ubuntu Server) may require you to install it manually.

  1. Open your terminal.
  2. Update your package list:
    sudo apt update
  3. Install the package:
    sudo apt install at
  4. Ensure the background scheduling service is running:
    sudo systemctl enable --now atd

Step 2: Schedule a Task

The at command works by dropping you into a special prompt where you can type the commands you want to execute later.

  1. Type at followed by the time you want the task to run. For example, to run a task at 10:30 PM, type:
    at 10:30 PM
    (You can also use natural language like at now + 2 hours or at noon tomorrow).
  2. Press Enter. You will notice your prompt changes to a at> symbol.
  3. Type the command you want to run. For example:
    bash /home/user/backup_script.sh
  4. Press Enter to move to a new line if you want to add more commands.
  5. When you are finished adding commands, press Ctrl + D.

The terminal will display an <EOT> (End of Text) symbol and confirm that your job has been scheduled with a specific job number.

How to View and Cancel Scheduled Tasks

If you change your mind, you can easily view your pending jobs and cancel them before they execute.

  1. To view a list of all your scheduled tasks, type:
    atq
  2. The terminal will output a list of jobs. Note the Job Number on the far left.
  3. To cancel a specific task, use the atrm command followed by the job number. For example, to cancel job number 3, type:
    atrm 3

Simplifying Automation

By using the at command, you eliminate the hassle of editing crontab files for tasks that only need to happen once. It is a powerful, elegant solution that allows you to confidently queue up system maintenance, updates, or script executions exactly when you need them.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.