Open In App

How to Schedule Tasks Using at Command in Linux

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The at command in Linux is used to schedule one-time tasks to be executed at a specified time in the future. It allows users to submit a command or script for execution at a later time, offering a convenient way to automate tasks without the need for complex cron jobs. The scheduled jobs are managed by the atd (at daemon) service, which runs in the background and executes the queued tasks at the specified times. In this article, we will explore the at command and the atd service used by the at command in Linux.

at command in Linux

The at command in Linux is used for scheduling one-time tasks to be executed at a specified time. Users can submit commands or scripts, and the atd daemon manages the execution of these scheduled jobs. It provides a simple way to automate future tasks without the need for complex cron expressions.

Syntax:

The basic syntax of the at command is as follows:

at [-q queue] [-f file] [-mldv] TIME
  • -q queue: Specifies the job queue (default is “a”).
  • -f file: Reads the job from the specified file.
  • -m: Sends an email to the user after the job has been executed.
  • -l: Lists the user’s pending jobs.
  • -d: Deletes a pending job.
  • -v: Displays the time of job execution.

How to Schedule Tasks Using at Command in Linux

Example 1: Scheduling a command to run at a specific time:

at 2:30 PM
at> echo "Hello, World!" > ~/hello.txt
at> Ctrl+D

This example schedules the echo command to write “Hello, World!” to a file at 2:30 PM.

Scheduling Command to run at specific time

Example 2: Using a file to specify commands:

$ echo "ls -l" > myscript
$ at 10:00 PM -f myscript

In this example, a script file (myscript) containing the ls -l command is scheduled to run at 10:00 PM.

Using a file to specify commands

Example 3:System Shutdown at Specific Date:

at 11:45pm July 31
at> shutdown now

In this example , the computer will shutdown on a specific date

Pasted-image

Example 4 : Delete a file on a specific Time:

at 11:45pm
at> rm hello.txt

In this example the hello.txt file will be removed at 11:45PM today

Pasted-image-1

Which service is used by at command in Linux?

The at command in Linux is used to schedule one-time tasks to be executed at a specified time in the future. It utilizes the atd (at daemon) service, which runs in the background and manages the execution of these scheduled jobs. Users can use the at command to submit commands or scripts along with the desired execution time, and atd ensures their execution at the specified time.

How atd service manages at command in Linux?

The atd (at daemon) in Linux manages the execution of commands scheduled using the at command. When a user submits a job using the at command, the job details are stored in the /var/spool/at directory. atd periodically checks this directory for pending jobs and, when the scheduled time arrives, it executes the jobs on behalf of the user. atd ensures the proper handling of scheduled tasks, managing their execution without requiring continuous user involvement, making it an efficient solution for one-time job scheduling in Linux systems.

Components of atd service:

  1. Job Queue: The atd service maintains a queue of jobs scheduled to run at specific times. Each job is associated with a particular time of execution.
  2. Scheduler: The scheduler component atd checks the scheduled jobs in the queue and ensures that tasks are executed at the specified times.
  3. Executor: When the scheduled time arrives, the executor component atd is responsible for launching and executing the specified commands or scripts associated with each job.
  4. Time Handling: The atd service manages the timing aspects of job execution. It calculates the time differences between the current time and the scheduled execution time to ensure accurate job execution.
  5. Logging and Reporting: atd may include components for logging and reporting. It logs information about job submissions, executions, and errors. Users can often check the status and history of their scheduled jobs.

Frequently Asked Questions on Service Used by at command in Linux -FAQs

How does the at command differ from cron in Linux?

at is used for one-time task scheduling, while cron is for recurring tasks at fixed intervals.

Can I check the status of my scheduled jobs?

Yes, use atq to list your pending at jobs and at -c <job_id> to view the details of a specific job.

What happens if my system is rebooted before an at job execute?

atd persists scheduled jobs across reboots, ensuring they are still executed at the specified times.

How can I remove a scheduled at job?

Use the atrm command followed by the job ID to delete a specific job, e.g., atrm 1.

Can I schedule at jobs as a different user?

Yes, use the -u option with the at command to specify the username, like at -u username TIME.

Conclusion

In conclusion, the at command in Linux provides a solution for scheduling one-time tasks efficiently, using the atd service for job management. Users can easily automate future tasks without the complexity of cron jobs. Understanding the syntax and examples helps the scheduling process, while monitoring and troubleshooting can be performed through commands like systemctl status atd.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads