How to Use the Linux at Command {9 Examples} – phoenixNAP

Introduction

The at command is a Linux command-line utility used to schedule a job for later execution. The utility reads commands from the standard input and groups them into an at job, which runs only once.

The alternative for at is a cron job. However, while at jobs run only once, cron jobs are recurring events.

In this tutorial, you will learn how to use the at command and see useful examples for programming commands.

Prerequisites

A

  • system running
  • Linux.

  • A user account with sudo privileges

.

Install

the at command

Depending on the Linux distribution you are using, the at command may

not be preinstalled.

Check if at is installed by entering the command name

in the terminal:

If the utility is not preinstalled, the output message indicates Command ‘at’ not found.

Install on Ubuntu

and Debian

Follow the steps below to install on Ubuntu or Debian:

1. Update the package repository:

sudo apt update

2. Install the at command by running

: sudo apt install in Install on CentOS and Fedora

Follow the steps below to install on CentOS or Fedora:

1. Update the package repository:

sudo yum -y update

2.

Run the following command to install on: sudo yum install in

Enable Scheduling Daemon

The scheduling daemon runs

in the background and runs scheduled jobs on time

. Run the following command after installing the at package to enable the atd programming daemon and

configure it to start at system boot. sudo systemctl enable -now atd

Linux at Command syntax

and options The

at command syntax is:

at [option] runtime

The options allow you to view or delete scheduled jobs and customize the job schedule in command. The available options are:

-V-q [queue][queue]a

zAZaat b batch batch batch atq-m-f [file][file]-b batch batch -latq-d atrm-v Thu Feb 20 14:50:00 1997-c-t [time_arg][time_arg][[CC]YY]MMDDhhmm[

runtime] Time

expressions Schedule a job using absolute time expressions or time expressions

relative to the job configuration time. The

absolute time expressions available are:

  • YYMMDDhhmm[.ss]. Specify a year, month, day, hour, minute, and optionally abbreviated seconds.
  • CCYYMMDDhhmm[.ss]. Specify a full year, month, day, hour, minute, and optionally seconds.
  • now. Indicates the current day and time and immediate execution.
  • midnight. Indicates 00:00 AM.
  • midday. Indicating 12:00 PM.
  • tea. Interpreted as 4 PM.
  • AM. Indicate the time before 12:00 PM.
  • PM. Indicate the time after 12:00 PM.
  • today. The current day.
  • tomorrow. The day after the current day.

For example, the following command schedules an echo command invocation at 5PM: echo

“Hello” | at 5PM Specify a relative time expression by

adding a plus sign (+), the amount, and one of the following options:

  • minutes
  • hours
  • days
  • weeks
  • months
  • years

Use time expressions such as morning or Tuesday , schedule the jobs on those days at the current time. Further specify the time using the + character.

For example, the following command schedules an echo command invocation five minutes after scheduling the job: echo

“Hello” | currently +5 minutes

Environment

The value of the SHELL environment variable determines which shell (bash, zsh, and so on) the at command uses to run the job. When scheduling the job, the command warns the user which shell to use during execution.

Linux at Command Examples

The following section shows useful examples of commands

. Schedule a job

interactively

The at command allows you to schedule a job using the interactive indicator. Open the interactive message with the following syntax:

at [runtime]

The interactive prompt allows you to enter which commands to execute at the specified time. The command also prints a warning indicating which shell the command will use.

Exit the interactive message and save the scheduled work by pressing Ctrl + D. Cancel the job with Ctrl + C.

For example:

In the example above, we schedule a job for 5 PM, which opens the interactive message. The scheduled job is to run the echo command and the utility uses the standard bash shell.

Pipe

a job to into

Schedule a job without the interactive symbol in by channeling commands to at and specifying the runtime. Use echo or printf to pass commands to at.

For example:

echo “command_to_run” | at [runtime]

To schedule an at job that sends the output of the echo command to a file, use: echo

“hello” >> example.txt | At this point Use the cat

command to check if the file exists to make sure the job was run: cat example.txt

View a scheduled job The -c option displays the contents of

a previously scheduled job

. The option is useful if you forget what the job was or want to check the scheduled time.

View the contents of

a job scheduled in using the following syntax: in

-c [job_number] Get the job number by first running the

atq command to list

all pending jobs.

For example:

In the example above, we first request a list of all pending jobs to get the job numbers and then display the contents of job 14 in the standard output. List of

scheduled

jobs

The at utility allows you to review which jobs are still in the queue and pending execution. There are two ways to view pending jobs

:

  • Run the atq command
  • . Use the -l option. For example: at

  • -l

The output displays the pending job number, date, time, year, and queue for the current user. To list the backlogs for all users, run the command with sudo.

sudo atq

In the example above, running atq displays pending jobs for the current user only. When you run atq with sudo, pending jobs are displayed for all users.

Send an email notification

about

execution The at utility sends email notifications about completed jobs by default. Email notifications require a working email address configured for your user account, and the system must be able to send emails.

To instruct at to send e-mail notifications upon job completion, even if there is no output, specify the -m option.

For example: rm example

.txt | at -m tomorrow

The above command schedules the work for tomorrow’s current time. Once completed, the at utility notifies the user using the email address configured for the bosko user.

Run jobs without notification

To avoid receiving email notifications about completed jobs, specify the –M option when scheduling an AT job.

For example:

echo “shutdown -h now” | at -M 00:00

In the example above, we schedule a system shutdown for midnight and use the –M option to prevent the utility from sending the email notification

.

Remove a

scheduled job

To remove scheduled jobs on, specify the –r option or run the atrm command together with the job ID. Get the job ID by running atq or using the -l option.

In the following example, we delete job 12

:

Restrict

users The at

package uses two files to restrict access to the at and batch command: /etc /at.allow/etc

  • /at.deny

Edit the files to allow only certain users to create, delete, or display pending jobs in they. Files are a list of user names that can or cannot access at commands. A newline character separates user names, and no blank spaces are allowed.

Edit the /etc/at.deny file with sudo privileges to deny a user access to the at commands. In the following example, we edit the file using the vi editor

: sudo vi /etc/at.deny After adding a user name to the

list, running the at command with the restricted user account results in the following message:

Run jobs when system load allows it

The batch command (option -b) schedules an at Job to run when the average system load drops below 1.5. Running jobs when the system load is low prevents the consumption of all system resources and ensures optimal system operation.

For example, the following job runs as soon as the system load drops below 1.5: batch echo

“Hello world!” >> hello.txt

Conclusion

This guide showed how to install and use the at command on Linux. Use the at utility to schedule one-time jobs and avoid forgetting to run them later. The utility is an alternative to cron jobs, with a simpler syntax and no recurring execution.

For other useful Linux commands,

check out our Linux command cheat sheet, with all the important Linux commands in one place.

Contact US