Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux at Command: Execute Tasks Regularly

In this tutorial, we'll learn about the at command in Linux and how to use it for scheduling one-time tasks.

  • Introduction to the at command

The at command in Linux allows you to schedule a task to be executed at a specific time in the future. This command is useful for running one-time tasks such as scripts, updates, or other operations that need to be executed at a specified time.

  • Installing the at package

On some Linux distributions, the at package may not be installed by default. You can install it using the package manager:

  • On Ubuntu/Debian-based systems:

    sudo apt-get update
    sudo apt-get install at
    
  • On CentOS/RHEL-based systems:

    sudo yum install at
    
  • Using the at command

To schedule a task with the at command, simply type at followed by the time when the task should be executed. The time can be specified in various formats:

  • Absolute time (HH:MM):

    at 14:30
    
  • Relative time (e.g., "now + 1 hour"):

    at now + 1 hour
    
  • Time with a date (e.g., "tomorrow 14:30"):

    at tomorrow 14:30
    

After specifying the time, you'll be prompted to enter the commands to be executed at that time. Enter the commands, press Ctrl-D to end the input, and the task will be scheduled.

Example:

$ at 14:30
warning: commands will be executed using /bin/sh
at> echo "Hello, World!" > /tmp/hello.txt
at> <EOT>
job 1 at Mon Sep 20 14:30:00 2021

This example schedules a task to create a file named hello.txt in the /tmp directory with the contents "Hello, World!" at 14:30.

  • Listing scheduled tasks

To list your scheduled tasks, use the atq command:

atq

This will display a list of scheduled tasks with their job IDs, execution times, and queue names.

  • Removing scheduled tasks

To remove a scheduled task, use the atrm command followed by the job ID:

atrm <job-id>

Replace <job-id> with the ID of the task you want to remove. This will delete the task from the at queue.

  • Viewing the contents of a scheduled task

To view the commands that will be executed by a scheduled task, use the at -c command followed by the job ID:

at -c <job-id>

Replace <job-id> with the ID of the task you want to view. This will display the commands and the environment variables set for the task.

By understanding and using the at command in Linux, you can effectively schedule one-time tasks to be executed at specific times in the future, automating various operations and making your system more efficient.

  1. Scheduling tasks with at in Linux:

    • Use the at command to schedule tasks to run once.
    at 10:30 PM
    
  2. Executing commands at a specific time in Linux:

    • Specify the time to execute a command using at.
    at 2:00 AM < command-to-run
    
  3. Using at command for one-time tasks in Linux:

    • Schedule a one-time task using the at command.
    at now + 1 hour < command-to-run
    
  4. Recurring tasks with at command in Linux:

    • Create a script to run periodically using the at command.
    echo "command-to-run" | at now + 1 day
    
  5. Specifying date and time formats with at in Linux:

    • Define date and time formats when scheduling tasks.
    at -m 10:30 PM
    
  6. Viewing and managing scheduled jobs with atq:

    • Use atq to view a list of scheduled jobs.
    atq
    
  7. Removing scheduled jobs with atrm in Linux:

    • Remove a scheduled job using atrm.
    atrm job-number
    
  8. Checking at command history in Linux:

    • Review the at command history for executed tasks.
    tail -f /var/log/at.log
    
  9. Redirecting output and errors with at in Linux:

    • Redirect standard output and errors to a file.
    at now + 1 hour < command-to-run > output.log 2>&1
    
  10. Running scripts with at command in Linux:

    • Execute a script using the at command.
    at now + 1 hour -f script.sh
    
  11. Batch processing with at in Linux:

    • Schedule multiple commands or tasks as a batch using at.
    at now + 1 hour <<EOF
    command-1
    command-2
    EOF
    
  12. Combining cron and at for task scheduling in Linux:

    • Use cron for recurring tasks and at for one-time or ad-hoc tasks.
    0 3 * * * /path/to/daily-script.sh