at - Linux


Overview

The at command in Linux schedules commands and scripts to be executed at a later time. Its primary use is to defer tasks that do not need immediate attention, allowing for better time and resource management on a system. Most effective in automating maintenance, backups, or any system tasks that can be run asynchronously.

Syntax

The basic syntax of the at command is as follows:

at [options] time [date]

You can specify commands either from stdin or through a script file redirected into at. Timings can be specific, like 5pm, or relative, such as now + 1 hour.

Options/Flags

  • -v: Display the time a job is scheduled for before it is executed.
  • -f file: Use the specified file for input instead of standard input.
  • -l: An alias for atq. Lists the user‘s pending jobs, unless the user is the superuser; in that case, it will list all jobs.
  • -d: An alias for atrm. Delete jobs, identified by their job number.
  • -c: Display the contents of a job on the standard output.
  • -q queue: Use the specified queue. a is the default, b to z and A to Z are usable with decreasing priorities.
  • -m: Send mail to the user when the job has completed even if there was no output.
  • -t time_arg: Specify the time for execution using the POSIX time format [[CCYYMMDDhhmm]].

Examples

  1. Schedule a script to run at 5 PM today:

    at 5pm today -f script.sh
    
  2. List all pending jobs for the current user:

    at -l
    
  3. Delete a job with job number 5:

    at -d 5
    
  4. Schedule a command to run in 30 minutes:

    echo "echo Hello World" | at now + 30 minutes
    

Common Issues

  • Permission Denied: Users might encounter permissions issues if the at.allow or at.deny files are configured to restrict access.
  • Syntax Errors: Incorrect time formats can lead to jobs not being scheduled. Always check the format if commands don’t execute as expected.

Integration

Combine at with scripts to perform nightly backups:

echo "/path/to/backup/script.sh" | at 02:00

Chain commands using a script file:

echo "command1 && command2" > temp_script.sh
cat temp_script.sh | at now + 1 hour
  • cron: Used for scheduled tasks but requires editing crontab files.
  • batch: Executes jobs when system load levels permit.
  • atq: Lists the user’s pending jobs.
  • atrm: Deletes jobs, specified by their job number.

By understanding the usage and flexibility of the at command, users can optimize and schedule tasks effectively in their daily operations or system maintenance routines.