Exploring the at Command in Linux

The at command in Linux is used to schedule tasks to run once at a specific time in the future. This command helps us automate the tasks by setting up commands or scripts to run at a scheduled time.  The at command reads commands from standard input (stdin) and groups them into an at job, which executes only once at the scheduled time. This functionality makes it a useful tool for handling one-time scheduled tasks on Linux systems.

In this tutorial, we will walk you through different examples of Linux at command examples to schedule a job.

Installing at Command in Linux

In most Linux distributions, the at command is not pre-installed by default. However, you can install it manually using the package manager specific to your distribution. For example, you can install the at command on Debian-based systems using apt package:

sudo apt install at

While on Red Hat-based systems, you can use the yum package as shown below:

sudo yum install at

After installing the at command, you can use it to schedule tasks to run at specific times.

How to Use the at Command in Linux

at is a command line utility in Linux that enables us to schedule one-time tasks. The cron command is an alternative to at, but it’s used for recurring tasks. Unlike at, which schedules a command to run once, cron executes commands at regular intervals.

You need to follow the below-given syntax to use this command in Linux:

at [option] time

Common Options and Time Expressions

You can replace “option” with one of the following flags depending on your specific scheduling needs:

  • q [queue]: The -q option in the command specifies the job queue (default is “a”).
  • f [file]: It reads the job from the given file.
  • m: It sends an email notification to the user after completing the job.
  • l: It lists your pending jobs.
  • d: It removes a pending job.
  • v: It checks when the job will run.

On the other hand, the “time” expression can be one of the following:

  • CCYYMMDDhhmm[.ss]: It denotes the full year, month, day, hour, minute, and optionally seconds.
  • YYMMDDhhmm[.ss]: It represents a shortened format for year, month, day, hour, minute, and optionally seconds.
  • now: It refers to the current time for immediate execution.
  • midnight: It represents 12:00 AM.
  • noon: It represents 12:00 PM.
  • teatime: Interpreted as 4:00 PM.
  • AM: It represents the time before 12:00 PM.
  • PM: It represents the time after 12:00 PM.
  • today: The present day.
  • tomorrow: The day following today.

at Command Manual Page

You can also access the at command’s manual page for a detailed understanding of this command. For this purpose, run the following command from the terminal:

man at

The manual page provides an overview of the at command, including its syntax, description, and available options:

at command manual page

Examples of Linux at Command

In this section, we will discuss several examples of the Linux at command to understand its working:

Example 1: Scheduling a Job to Run at a Specific Time

The following command schedules a reboot after three minutes from now:

echo "sudo reboot" | at now + 3 minutes
scheduling a job to run at a specific time

Note: You can learn more about the reboot command in our dedicated guide on restarting Ubuntu from the terminal.

Example 2: Listing the Scheduled Jobs 

Use the -l option with the at command to display all the jobs currently scheduled in the queue:

at -l

Alternatively, you can run the atq command to achieve the same functionality:

atq

The output shows that a job with the number 15 is pending:

listing the scheduled jobs

Example 3: Deleting a Scheduled Job 

We can use the at command with the -d option to delete any pending job from the queue. For instance, the following command deletes a Job 15 from the queue:

at -d 15

You can list the remaining pending jobs to confirm that a job has been deleted:

deleting a scheduled job 

Example 4: Sending an Email Notification on Execution of a Specific Job

We can execute the at command with the -m option to send an email to the user when a scheduled job runs:

cat sampleFile.txt | at -m midnight

This command schedules the execution of sampleFile.txt contents at midnight and sends an email upon completion:

sending an email notification on execution of a specific job

Example 5: Viewing a Specific Scheduled Job

You can run the at command with the -c option to view the contents of a job that was scheduled earlier. This can be helpful if you need to recall the details of the job or verify the scheduled time. The following command shows how to view a specific scheduled job using its job number:

at -c 16

The following output shows that the at command returns the content of the job 16:

Viewing a specific scheduled job

Example 6: Schedule a Job for the Next Tuesday

We can specify a day of the week with the at command to schedule a job at a specific day:

at Tuesday +25 minutes

This command schedules a task to run 25 minutes after the current time on the upcoming Tuesday:

schedule a job for the next tuesday

Example 7: Saving the Output of at Command to a File

We can also log the output of a scheduled job to a file using the at command:

echo "df -h > /home/anees/storageDetails.txt" | at now + 15 minutes

This command schedules a disk usage report to be saved in a file named storageDetails.txt after 15 minutes:

saving the output of at command to a file

This way, you can use the at command with any valid option or time expression to schedule tasks to run at specific times.

Conclusion

at is a command-line tool for scheduling one-time tasks in Linux. You can install it using package managers like apt or yum. Moreover, you can customize scheduling with valid options. In this article, we explored how to install at, various time expressions, and practical examples, including scheduling tasks, listing and deleting jobs, sending email notifications, and logging output.

The at command in Linux is a powerful tool for scheduling one-time tasks to run at a specific time. For seamless execution of scheduled tasks and improved system performance, consider Ultahost’s VDS hosting plans, which offer dedicated resources and optimal reliability for advanced operations.

FAQ

What is the at command used for in Linux?
How can I install the at command on a Debian-based system?
How do I install the at command on a Red Hat-based system?
How can I list scheduled jobs with the at command?
How do I delete a scheduled job using the at command?
Can I receive an email notification when a job is executed?
How do I view the contents of a specific scheduled job?

Related Post

How to Fix “ifconfig: Command not Found

Displaying the error "ifconfig: command not found" on a...

Mastering Process Management with Linux ps au

In Linux systems, processes are the backbone of system ...

Exploring Linux cut Command with Examples

The Linux cut command is a powerful utility that allows...

How to Install Maldet on Linux Server

Linux Malware Detect shorts for Maldet is a powerful ma...

How to Check Kali Linux Version

Kali Linux is a Debian-based Linux distribution aimed a...

How to Fix Systemctl Command Not Found Error

Linux operating systems use the "systemctl" command to ...

Leave a Comment