One-time Scheduling with at

LinuxBeginner
Practice Now

Introduction

While cron is ideal for recurring jobs (daily, hourly), sometimes you need to schedule a task to run only once at a specific time in the future. For example, you might want to restart a service at 2:00 AM tonight, or take a database snapshot in 30 minutes.

For these one-off tasks, Linux provides the at utility.

The at command reads commands from standard input or a file and executes them at a later time. This is useful when the task is delayed but not recurring, which is different from cron's repeating schedule model.

So the main conceptual difference is simple: cron describes repeating schedules, while at queues a one-time future job. Both are automation tools, but they solve different timing problems.

In this lab, you will:

  1. Install Prerequisites: Ensure the at utility and daemon are running.
  2. Schedule a Job: Use at to queue a command for future execution.
  3. Inspect the Queue: Use atq to list pending jobs.
  4. Manage Jobs: Use atrm to remove a job from the queue.

Install the at Utility

The at command is not always installed by default on all Linux distributions. We must first verify it is present and that its background service is running.

Like cron, at depends on a daemon. The command you type creates a queued job definition, and the background service is responsible for actually running that job later.

  1. Install the package:
sudo apt-get install -y at
  1. The at system relies on a background daemon called atd to execute the scheduled commands. Start the service:
sudo service atd start
  1. Verify that the service is active:
sudo service atd status

You should see output indicating that atd is running.

Schedule a Job

Now that the system is ready, let's verify that at works by scheduling a simple task to create a file 5 minutes from now.

To start, run the at command followed by the desired time:

at now + 5 minutes

The prompt will change to at>, indicating that it is waiting for your input. Type the command you want to execute:

This interface is different from cron because at is collecting the future job's command text interactively. You are effectively writing the body of a one-time task and submitting it to the queue.

echo "Hello LabEx" > /home/labex/project/hello.txt

To Save and Exit:

  • Press Ctrl + D.

You should see <EOT> (End of Transmission) and a confirmation message like job 1 at ....

This confirms your job has been submitted to the queue.

List Pending Jobs (atq)

After scheduling a job, you may want to verify that it is in the queue or check its Job ID.

The command to list the queue is atq:

atq

Output format typically looks like: 1 Mon Oct 23 10:05:00 2023 a labex

Breakdown:

  • 1: The Job ID. You need this number to remove the job.
  • Date/Time: When the job is scheduled to run.
  • User: The user who owns the job.

The queue view is important because scheduled work is easy to forget once submitted. atq gives you a way to inspect pending tasks before they execute.

If the queue is empty, the job may have already run or failed to submit.

Cancel a Job (atrm)

If you change your mind or realize you made a mistake, you can remove a job from the queue before it runs.

Use the atrm command followed by the Job ID.

First, verify the Job ID using atq (it was likely 1 in the previous step). Then run:

atrm 1

Now verify the queue is empty:

atq

The output should show nothing, confirming the job has been cancelled. No file will be created.

That cancellation step reinforces that queued jobs are manageable objects, not just fire-and-forget commands. You can inspect them, identify them by ID, and remove them before execution if your plan changes.

Summary

In this lab, you learned how to manage one-time tasks:

  1. Installation: Setting up at and atd.
  2. Scheduling (at): Using syntax like at now + 5 minutes to queue future commands.
  3. Listing (atq): Inspecting the queue to see pending Job IDs and execution times.
  4. Removal (atrm): Deleting specific jobs from the queue using their ID.

Use cron for recurring schedules, and at for single, delayed executions.