at Command: Tutorial & Examples

Execute a command at a specified time

The at command is a Linux utility that allows you to schedule a command or script to run at a specific time in the future. It is useful for scheduling tasks that need to be performed on a one-time or recurring basis, such as backups, system maintenance, or email notifications.

To use at, you must first specify a time and date for the task to run. You can do this by providing a numerical value for the time, such as 14:30 for 2:30 PM, or by using keywords such as midnight, noon, or teatime (4 PM). You can also specify a relative time, such as now + 1 hour or next Monday.

Once you have specified the time, you can enter the command or script that you want to run. You can enter the command directly on the command line, or you can specify a script file by using the -f option. For example:

at 14:30
at> /path/to/script.sh

or

at -f /path/to/script.sh 14:30

When you are finished entering the command or script, press CTRL+D to submit it to the at daemon. The at daemon will execute the command or script at the specified time.

You can view a list of tasks scheduled to run with at by running the atq command. You can cancel a scheduled task by running atrm followed by the task's ID, which can be found by running atq.

Note that the at command is not enabled by default on all Linux distributions. You may need to install the at package and configure the atd daemon to start automatically at boot before you can use it.

How at works

The at command works by passing the specified command to the atd daemon, which manages and executes scheduled tasks in the background. When the specified time arrives, atd retrieves the command from its internal queue and runs it in a non-interactive shell.

What at does

The primary function of the at command is to schedule commands or scripts to be run at a designated time. This makes it a valuable tool for automating tasks that do not require user interaction.

What at is used for

Common use cases for the at command include:

  • Scheduling one-time backups
  • Running maintenance scripts during off-peak hours
  • Sending automated email notifications
  • Executing system updates or patches

Why at is important

The at command is important for system administrators and users who want to automate tasks without needing to set up complex cron jobs. This simplicity makes it an accessible tool for managing time-based tasks.

Common command line parameters

While the basic usage of the at command is straightforward, there are several options available: - -f: Specifies a file containing the command(s) to execute. - -l: Lists the jobs in the at queue. - -r: Removes jobs from the at queue. - -V: Displays the version of the at command.

Potential problems and pitfalls

Some common issues users may encounter when using at include:

  • Jobs not executing due to permission issues. Ensure that the user has permission to run the specified command.
  • The atd daemon not running. Verify that the service is active and running on your system.
  • Incorrect time format. Ensure that the specified time is valid and properly formatted.

Common errors and troubleshooting

When scheduling tasks with at, users may encounter errors such as:

  • "Permission denied": This may occur if the user does not have the necessary permissions to execute the command.
  • "No such job": This indicates that the job ID provided to atrm does not exist. Verify the job ID using atq.

Security considerations

When using the at command, be mindful of security implications:

  • Ensure that only trusted users can access the at command to prevent unauthorized task scheduling.
  • Regularly review the scheduled tasks with atq to monitor for any unexpected commands.

Cheatsheet

Basic usage examples for the at command:

at 14:30
at> echo "Backup completed" >> /path/to/backup.log
CTRL+D

at -f /path/to/script.sh 14:30

To view scheduled tasks:

atq

To remove a scheduled task:

atrm <job_id>

Real-world use cases

  1. Scheduling a backup:

    at 02:00
    at> rsync -av /home/user /backup/`date +%Y-%m-%d`
    
  2. Running a maintenance script:

    at now + 1 hour
    at> /path/to/maintenance.sh
    
  3. Sending a notification email:

    at now + 15 minutes
    at> echo "System will reboot in 10 minutes" | mail -s "Reboot Notification" user@example.com
    

See also

The text above is licensed under CC BY-SA 4.0 CC BY SA