at Command in Linux with Examples

In the world of Linux operating systems, there exists a powerful tool known as the “AT command.” The AT command provides users with the ability to schedule tasks to be executed at a later time, offering a convenient way to automate processes without manual intervention. Whether you need to run a script, execute a command, or perform a system task at a specific time, the AT command provides the flexibility to accomplish these tasks efficiently. In this article, we will delve into the details of the AT command in Linux, exploring its syntax, options, and practical examples.

Introduction to the AT Command:

The AT command allows users to schedule one-time tasks or recurring jobs at a specific time and date. It is particularly useful for automating system maintenance, backups, software updates, and various administrative tasks. The AT command works by queuing commands to be executed by the system’s job scheduler at the specified time.

When to use the ‘at’ utility

The at utility is best used for scheduling one-time tasks to run at a specific time in the future. Here are some scenarios where at is particularly useful:

  • Delayed Execution: Running a command or script after a certain delay without needing repeated execution.
  • System Maintenance: Scheduling maintenance tasks like backups or updates during off-peak hours.
  • Reminders: Sending reminders or notifications at a future time.
  • Resource Management: Running resource-intensive tasks at a time when system usage is low.

Example:

echo "echo 'Backup complete'" | at 2am

This schedules a one-time task to echo “Backup complete” at 2 AM.

Linux at Command Syntax and Options

The basic syntax of the AT command is straightforward:

at [-V] [-q queue] [-f file] [-mldbv] timespec
  • -V: Display version information.
  • -q queue: Specify the job queue. The default queue is ‘a’.
  • -f file: Read commands from the specified file.
  • -m: Send mail to the user when the job has been completed.
  • -l: List the at jobs in the queue.
  • -d: Delete the at job specified by the job number.
  • -b: Submit a batch job. This is the default behavior.
  • -v: Display verbose information about the job.
  • timespec: Specify the time and date when the job should be executed.

Installation of at command

For Ubuntu/Debian :

sudo apt-get update
sudo apt-get install at

For CentOS/Fedora :

sudo yum install at

Examples of Using the AT Command:

Listing Scheduled Jobs:

To list all scheduled jobs in the AT queue, you can use the `-l` option:

at -l

or

atq

This command will display a list of all pending jobs along with their job numbers.

Deleting a Scheduled Job:

If you need to remove a scheduled job from the queue, you can do so by specifying its job number with the -d option:

at -d 1

This command will delete the job with job number 1 from the AT queue.

Scheduling a Job for the Coming Monday

To schedule a job for the coming Monday at a time twenty minutes later than the current time, the syntax would be:

at Monday +20 minutes
  • at: The command to schedule a one-time task.
  • Monday: Specifies the next Monday as the starting point.
  • +20 minutes: Adds 20 minutes to the specified starting point.

This command instructs the system to execute the specified job on the upcoming Monday, twenty minutes after the current time.

Scheduling a Job for a Specific Date and Time

To schedule a job to run at 1:45 on August 12, 2020, the command is:

at 1:45 081220
  • 1:45: The time at which the command will be executed.
  • 081220: The date in MMDDYY format, which stands for August 12, 2020

Here, the command specifies the exact date and time when the job should be executed, in the format of hour:minute monthdate.

Scheduling a Job for a Future Time

If you need to schedule a job to run at 3 PM four days from the current time, the command would be:

at 3pm + 4 days
  • at 3pm: Specifies the time of day when the task should run, which is 3 PM.
  • + 4 days: Adds four days to the current date to determine the execution date.

This command schedules the job to be executed at 3 PM, four days from the current time.

Scheduling a System Shutdown

To schedule a job to shutdown the system at 4:30 PM today, you can use the following command:

echo "shutdown -h now" | at -m 4:30
  • echo "shutdown -h now": This part creates a command that shuts down the system immediately.
  • |: The pipe operator (|) passes the output of the first command (echo "shutdown -h now") as input to the next command.
  • at -m 4:30: This schedules the provided command to run at 4:30 (AM or PM). The -m flag tells at to send a mail to the user when the job is done (if mail is configured).

This command pipes the shutdown command to the AT command, specifying that the system should be shut down at 4:30 PM, and the user should receive an email notification when the job is completed.

Scheduling a Job for a Future Time Relative to Now

To schedule a job to run five hours from the current time, the command would be:

at now +5 hours
  • at: This is the command used to schedule tasks for future execution.
  • now +5 hours: This specifies the time at which the task should be executed.
    • now refers to the current time.
    • +5 hours adds 5 hours to the current time.

scheduling a job using at command

This command schedules the job to be executed five hours from the current time.

Different Method to delete job

at -r or atrm command is used to deletes job , here used to deletes job 11.

 at -r 11
  • at is the command-line utility for scheduling one-time tasks.
  • -r (or --remove) is an option to remove a scheduled job.
  • 11 is the job number that you want to remove from the at queue.

or

atrm 11

deleting a job using at command

Using a Shell Script File

Let’s say you want to create a script named backup.sh that performs a backup operation:

  • Create the Script File: Open a text editor and create a file named backup.sh:
nano backup.sh
  • Write the Script: Inside backup.sh, write your backup script.

For example:

#!/bin/bash
mkdir -p /backup
cp /path/to/important/files/* /backup
echo "Backup completed successfully!"
  • Make the Script Executable: Make the script executable with the following command.
chmod +x backup.sh
  • Schedule the Script with at Command: Now, you can schedule this script to run at a specific time using the at command
at 3pm + 4 days < backup.sh

This will schedule the backup.sh script to run at 3 PM four days from the current date.

Difference between at and cron

Feature at cron
Purpose Schedule one-time tasks Schedule recurring tasks
Execution Runs a command or script once at a specified time Runs commands or scripts at specified intervals
Syntax at [time] Uses crontab syntax (* * * * * command)
Interval Single execution Repeated execution (minute, hour, day, month, day of week)
Use Cases Delayed tasks, maintenance tasks, one-off jobs Regular backups, scheduled reports, periodic tasks
Job Management View with atq, remove with atrm Edit with crontab -e, list with crontab -l, remove with crontab -r
Configuration File No separate file, commands are entered directly Uses crontab file or /etc/crontab
User Level Control Yes, user-specific job scheduling Yes, user-specific crontabs
Examples `echo “echo ‘Task done'” at 5pm`

Conclusion

The AT command in Linux offers a convenient way to schedule tasks for execution at a later time. By understanding its syntax and options, users can automate various system tasks and streamline their workflow effectively. Whether it’s a one-time task or a recurring job, the AT command provides the flexibility and reliability needed to manage scheduled tasks efficiently in Linux environments. By incorporating the AT command into your system administration toolkit, you can enhance productivity and ensure timely execution of critical tasks.

at Command in Linux – FAQs

What is the at command in Linux?

The at command in Linux is used to schedule a command or script to be executed at a specified time in the future. It allows users to run commands once at a specific time without manual intervention.

What is the run command with at?

To schedule a command with at:

at 12:18
echo "hello world" > /tmp/at_test_output
<Ctrl+D>

What is the at command scheduled task in Linux?

The at command in Linux schedules a one-time task to run at a specified future time. It allows users to execute commands or scripts at a later time, using the /bin/sh shell by default.

What is the use of ‘- T command in Linux?

The at -t command in Linux schedules a job to be executed at a specified time, using the time format [CC]YYMMDDhhmm[.ss]. This allows for precise scheduling of tasks down to the second.

What is the at command used for?

The at command is used to schedule a one-time execution of a command or script at a specified time in the future. It allows users to automate tasks without requiring them to be present when the task runs.

What is the difference between at and cron in Linux?

The at command schedules a one-time task for a specific time, while cron schedules recurring tasks at specified intervals. at is used for single, future tasks, whereas cron manages regular, repeated tasks.



Contact Us