Logo

Schedule Tasks with the Linux at Command: A Comprehensive Guide

Try this guide with our instant dedicated server for as low as 40 Euros

linux at command

Whether you need backups to run at midnight or a script to trigger at sunrise, the at command puts you in control.

The at command takes the manual intervention out of the picture and lets your Linux system handle the heavy lifting while you focus on what matters. 

The at command is a utility in Linux that schedules a single execution of a job at a later time. It accepts input directly from the user and compiles it into an at job designed to run just once.

In this tutorial, we will discuss the at command and some critical use cases. We will start with an introduction to the at command and then go into the description of the use cases.

Table Of Contents

  1. What is the at Command?
  2. How to Install the at Command
  3. Linux at Command Use Cases
    1. Example #1: Schedule a Job Interactively
    2. Example #2: Piping a Job
    3. Example #3: Schedule a Job That Writes to a File
    4. Example #4: Verify Job Execution
    5. Example #5: Viewing the Contents of a Scheduled Job
    6. Example #6: Execute Jobs Silently with the at Command
    7. Example #7: Cancel a Scheduled Job
    8. Example #8: Restrict User Access
  4. Schedule Jobs with batch
  5. Conclusion
  6. FAQs

What is the at Command?

The at command in Linux is a utility that schedules one-time tasks to be executed at a specified time in the future. Unlike cron, which is used for recurring tasks, the at command is ideal for tasks that need to be run just once. 

It is particularly useful for system maintenance, automated backups, or any other task that needs to occur at a specific future time without manual user intervention.

The Basic Syntax

The basic syntax for the at command is as follows:

at [options] TIME

Here,

[options]: (Optional) Flags that modify the behavior of at

TIME: This specifies when the scheduled task should be executed. 

Let us now discuss it in detail. 

Options

The following table illustrates some of the common options used with the at command.

options and description

Basic Time Formats

When scheduling tasks with the at command, specifying the execution time is essential. TIME specifies when the system should execute the job. It can be specified in various formats, such as now + 1 minute, 5 pm tomorrow, or 2:00 Jan 1.

Let us discuss some of the common formats.

A Specific Time

To specify the exact time, use the following formats.

hh:mm: Schedules the job at a specific hour and minute, using a 24-hour format. 

Example: at 14:30 schedules a job at 2:30 PM.

hh:mm AM/PM: Uses a 12-hour format with AM or PM designation. 

Example: at 10:30 PM.

Midnight, Noon, Teatime

Midnight: Schedules the job at 00:00.

Noon: Schedules the job at 12:00.

Teatime: Schedules the job at 16:00 (4:00 PM).

Relative Time

Now + Count units: Schedules the job for one day from the current time. Units can be minutes, hours, days, or weeks. 

Example: now + 1 day.

Date Specifications

Let us now discuss how to specify dates.

Specific Dates

To specify dates to schedule a task, use the following format.

MMDDYY, MM/DD/YY, DD.MM.YY: You can use different formats to specify the exact date. 

Example: To schedule the job for July 10, 2022, use 071022, 07/10/22, or 10.07.22.

Day of the Week

You can schedule a job for a specific day of the next week. 

Example: Friday 9:00 PM.

Combined Time and Date

You can combine both time and date to schedule a job more precisely.

HH:MM YYYY-MM-DD: Schedules at a specific time on a specific date. 

Example: 15:00 2023-12-25.

This schedules the job for December 25th, 2023 at 3 PM.

HH:MM AM/PM month day: Combines time with a more readable date format. Example: 3:00 PM July 11.

Now that you have an understanding of the at command, let us discuss how to install the utility and some of its use cases. However, before that, let us take a quick look at the prerequisites to working with the command. 

The Prerequisites 

Before diving into the installation and use cases, ensure you have the following.

  • A system running a mainstream Linux distribution.
  • A user account with sudo or administrative privilege.
  • Terminal or command line access

How to Install the at Command

The at command in Linux allows users to schedule tasks to run at a specific time.

Follow the steps below to install the at command.

Step #1: Verification

GIven its importance, chances are that the at command might be pre-installed on your Linux distribution. We recommend that you check the availability by executing the following command in the terminal:

# at

verification of at command

If the at command is not installed on your system, you will see the Command at not found warning. 

Step #2: Update Package Repository

Before installing the at command, update your system’s package repository to ensure your system has access to the latest software versions.

If you are running Ubuntu or any other Debian-based distribution, run this command:

# sudo apt update

Alternatively, use this command for CentOS or Fedora-based distributions:

#sudo yum -y update

Step #3: Install the at Command

Once you have updated your package repository, install the at command. 

Run the following command for Ubuntu and Debian-based systems:

# sudo apt install at


For CentOS or Fedora-based systems:, run the following command:

# sudo yum install at

The command installs the at utility on your system, allowing you to schedule tasks to be executed later.

Install the at Command

Step #4: Enable the atd Scheduling Daemon

After installing the at package, you need to enable and start the scheduling daemon, atd. It is a background service that needs to be enabled to ensure your scheduled tasks run smoothly. 

To enable the atd scheduling daemon and set it to start automatically on system boot, execute the following command:

# sudo systemctl enable --now atd

This command not only starts the atd service immediately but also configures it to launch automatically whenever the system boots up. This ensures that the scheduled tasks are always managed without the need for manual intervention each time the system starts.

sudo systemctl enable --now atd

The at command in Linux is used for scheduling commands to be executed once at a specified time in the future. 

Understanding the syntax and options of the at command is essential for effectively utilizing it for task scheduling.

Let us now discuss some of the practical use cases of at command. 

Linux at Command Use Cases

We will now cover several use cases that cover the complete range from basic job scheduling to more advanced applications of the Linux at command. 

Example #1: Schedule a Job Interactively

The at command allows for interactive scheduling. This allows users to enter commands directly into a prompt, ensuring they will execute at a specified time.

The basic syntax for interactive scheduling is:

at [runtime]

To schedule a job interactively using at command, follow these steps:

Open the interactive mode by specifying the desired execution time. 

For instance to schedule a job at 5 PM, run:

at [5pm]

You will see a prompt where you can type the commands you want to run at the scheduled time.

For instance, let’s create a simple backup log message.

# echo "Backup started" > /tmp/backup_log

Once you’ve entered all your commands, press Ctrl+D to save the job and exit the interactive mode. If you make a mistake or need to cancel, press Ctrl+C.

The interactive mode is useful for complex command sequences that need to run together at the scheduled time. Note that this mode uses the default shell specified in your environment variable to ensure compatibility with your existing shell scripts and configurations.

at 4 am

Example #2: Piping a job 

The at command also offers a way to schedule tasks directly from the command line without entering the interactive prompt. This option is ideal for quick and efficient one-time job creation. 

To pipe a job, use echo or printf to send commands directly to at by specifying the time of execution. The command syntax in this case will be as follows:

# echo "command_to_run" | at [runtime]

Example #3: Schedule a Job That Writes to a File

The at command empowers users to schedule tasks that interact with files, including writing data at specific times. 

For instance, you can use the following approach to schedule a job that outputs to a file at a specified time:

# echo "echo 'hello' >> redswitches.txt" | at now

Here,

Inner echo: This command constructs the actual task to be executed – appending hello to the file redswitches.txt using the >> operator.

Outer echo: This serves as a wrapper, sending the inner echo command to at.

at now: This schedules the job to run immediately.

This method of piping commands to at offers a straightforward way to schedule tasks efficiently without manual intervention during execution.

Example #4: Verify Job Execution

After scheduling a task with at, it’s essential to confirm its successful execution. 

We will consider the previous example where we scheduled a job to append hello to a redswitches file for verification.

# cat redswitches.txt

cat redswitches.txt

This command displays the contents of redswitches.txt to ensure that the echo command was executed and the text hello was appended as scheduled.

Example #5: Viewing the Contents of a Scheduled Job

The at command provides functionalities beyond just scheduling tasks. You can also view the contents of a previously scheduled job. This is particularly helpful if you need to recall the specific commands you’ve scheduled or verify the exact time a job is set to run.

Start by running the following command that displays all pending jobs, their job numbers, scheduled times, and dates:

#atq

Once you have the job number from the list provided by atq, you can view the specific details of any job using its job number. Use the job number in the following command syntax to view the details of a scheduled job:

# at -c [job_number]

For instance, to view the contents of job number 14, run:

# at -c 14

at -c 14

Example #6: Execute Jobs Silently with the at Command

The at command offers flexibility when it comes to email notifications about job completion. While these notifications can be helpful, there might be situations where you prefer a quieter approach. 

Use the -M option to prevent the at utility from sending any email alerts once the job is executed.

To suppress these notifications, run the following syntax:

# echo "command_to_execute" | at -M [scheduled_time]

For instance:

# echo "rm /var/log/old_logs.log" | at -M 3:00 AM

echo rm

Here,

rm /var/log/old_logs.log: Deletes an old log file.

-M: Prevents at from sending an email once the job is completed.

Scheduled Time: The task is set to run at 3:00 AM.

This method effectively schedules maintenance tasks to run without cluttering your inbox with completion notifications.

Example #7: Cancel a Scheduled Job 

If you need to cancel a job you’ve previously scheduled with the at command, use the atrm command, which requires the job ID of the task you want to delete.

Follow the steps below to cancel a scheduled job.

Identify the job ID of the task you wish to cancel. 

Use atq command (or at -l) to view all scheduled jobs and their IDs.

# atq

Once you have the job ID, remove the job using the atrm command followed by the job ID.

# atrm [Job ID]

For instance, to remove a job with the job ID 5, execute the following command:

# atrm 2

atrm 2

This command removes the scheduled job with the ID number 2.

This method allows you to easily manage and cancel scheduled tasks, ensuring that only the necessary jobs are executed.

Example #8: Restrict User Access 

In multi-user environments, managing who can leverage the at command becomes crucial. Linux provides a mechanism to control user access through two files: /etc/at.allow and /etc/at.allow.

/etc/at.allow: This file lists the usernames of users permitted to use at and batch commands. If this file exists, only the users listed in this file can use these commands.

/etc/at.deny: This file lists the usernames of users not allowed to use at and batch commands. If /etc/at.allow does not exist, the system checks this file to determine who is denied access. If neither of the files exists, only the superuser (root) has access to at command by default.

To permit specific users, edit the /etc/at.allow:

# sudo vi /etc/at.allow

Add one username per line to grant access.

To deny specific users, edit the /etc/at.deny:

# sudo vi /etc/at.deny

Add one username per line to restrict access.

Schedule Jobs with batch 

The batch command in Linux offers a unique approach to task scheduling, prioritizing system performance over immediate execution. 

Unlike at, which schedules jobs for a specific time, batch focuses on system load. It queues your task to run only when the system’s load average dips below a predefined threshold (typically 1.5). 

This ensures that resource-intensive tasks won’t interfere with more critical processes during peak usage periods.

To schedule a job with batch, simply pipe a command into it:

command_to_run | batch

This command schedules a job to compress a data directory when the system load is low enough not to disrupt other processes.

Before queuing tasks with the batch command, assessing the current system load is important. Use the uptime command to view the current system load.

# uptime

This shows the load average over the last 1, 5, and 15 minutes. If the first number is below 1.5, batch jobs will likely start executing.

uptime

Conclusion

To effectively manage system resources and ensure smooth operations, mastering Linux commands like at is essential. 

These tools allow for precise scheduling of tasks, either at specific times or when system loads are optimal, ensuring that your server runs efficiently.

FAQs 

Q. What is the rm command used for?

The rm (remove) command deletes files and directories from the filesystem. For instance, rm file.txt deletes the file file.txt, and rm -r folder deletes the folder named folder and all its contents.

Q. How do you use the cat command?

The cat (concatenate) command displays the content of files to the terminal. If you run cat file1.txt, it will show you what’s inside file1.txt.

Q. What does the current directory refer to?

The current directory is the directory in which you work in the terminal. It’s the default directory for any operations performed without specifying a path.

Q. What is meant by the current working directory?

A: Similar to the current directory, the working directory is the folder you operate in during your terminal session. Use pwd (print working directory) to see where you are currently.

Q. Who is the root user?

The root user is the administrative user in a Linux environment. This user has unrestricted access to all files and commands.

Q. How can I display the current time in the terminal?

Type date in the terminal to display the current system time and date.

Q. What does the mkdir command do?

The mkdir (make directory) command creates a new directory. For example, mkdir new_folder creates a directory named new_folder.

Q. How do file permissions work?

File permissions in Linux specify what the file owner, the group, and others can do with a file, such as read, write, or execute it.

Q. What is a package manager?

A package manager is software that automates installing, updating, and removing software packages. Examples include apt for Ubuntu and yum for CentOS.

Q. How is the head command used?

The head command displays the first few lines of any text file. By default, it shows the first 10 lines unless specified otherwise with -n followed by the number of lines you want.

Q. What does the sudo command do?

The sudo (superuser do) command lets a permitted user run commands with the privileges of another user, usually the superuser or root. This allows users to perform tasks that require administrative permissions.

Q. What is the command prompt?

The command prompt is the line in a terminal where you type your commands. It typically shows the current directory, user, and other information.

Q. How do you use the touch command?

The touch command creates a new file or updates the timestamps of an existing file. For example, touch newfile.txt creates an empty file named newfile.txt.

Q. What is the cp command?

The cp command copies files or directories. For example, cp original.txt copy.txt copies the content of original.txt into a new file called copy.txt.

Q. What does the pwd command do?

The pwd command displays the path of the current working directory.

Q. What does execution time refer to?

Execution time refers to the time a program or command takes to complete its task.

Q. What are one-time tasks?

One-time tasks are actions scheduled to execute just once at a specified time, typically managed by commands like at.

Q. What is a script file?

A script file is a set of commands saved in a file. When executed, it runs in the order it is written.

Q. What is an input file?

An input file provides data to a program during its execution. It is read by the program and can influence the program’s processing.

Try this guide with our instant dedicated server for as low as 40 Euros