Oct 23, 2023 6 min read

At Command in Linux

Utilize at command in Linux with our step-by-step tutorial. The at command schedules the execution of commands or scripts at a specific time.

At Command in Linux
Table of Contents

Introduction

Before we discuss At command in Linux, let's first understand-What is At Command ?

The at command in Linux allows users to schedule the execution of commands or scripts at a specified time in the future. It provides a way to automate tasks and perform them at specific moments without the need for manual intervention. With at, users can schedule one-time or recurring jobs, making it a powerful tool for task automation and management in Linux systems.

In this tutorial, we'll show you how to utilize at and its partner utilities batch, atq, and atrm to view, delete, and create jobs that will be run at a later time. We will also address a few FAQs on At command in Linux.

Installing at

at may or may not be present on your Linux system, depending on the distribution.

If at isn't already installed, you can do so using your distribution's package manager.

  • Install at on Ubuntu and Debian
sudo apt update 
sudo apt install at
  • Install at on CentOS and Fedora
sudo yum install at

Make sure that atd, the scheduling daemon is running and set to start on boot after installing the program:

sudo systemctl enable --now atd

How to Use the at Command

The at command has the following simplified syntax:

at [OPTION...] runtime

As a command-line parameter, the at command accepts the date and time (runtime) when you wish to run the task, as well as the command to run from standard input.

Let's create a job that will start at 9:00 a.m.:

at 09:00

When you press Enter, you'll see the at command prompt, which usually begins with at>. There's also a warning that tells you what shell the command will run in:

Output

warning: commands will be executed using /bin/sh
at>

Enter one or more commands that you'd want to run:

tar -xf /home/vegastack/file.tar.gz

Press Ctrl-D to exit the prompt and save the task after you've finished inputting the commands:

Output

at> <EOT>
job 4 at Fri Feb  5 09:00:00 2022

The task number, as well as the execution time and date, will be displayed by the command.

Aside from inputting the command at the prompt, there are additional ways to send the command you want to run. One method is to use echo and pipe the command to the following address:

echo "command_to_be_run" | at 09:00

Another choice is to use the following document:

at 09:00 <<END
command_to_be_run
END

Invoke the command with the -f option followed by the path of the file to read commands from a file instead of standard input. To create a job that runs the script /home/vega/script.sh, for example:

at 09:00 -f /home/vega/script.sh

If the command produces output, by default, at will send the user an email with the output after the operation is finished. To disable the email notification, run at with the -M option:

at 09:00 -M

Even if there is no output, use the -m option to send an email:

at 09:00 -m

batch Command

When the system load level allows, batch or its alias at -b schedules jobs and runs them in a batch queue. The jobs are run by default when the system load average is less than 1.5. When calling the atd daemon, you can specify the load value. Jobs will wait in the queue if the system load average is higher than the specified one.

To create a job with batch, enter the commands you want to run:

echo "command_to_be_run" | batch

Specifying the Execution Time

The at utility can handle a wide range of time constraints. You can set the time, date, and increment from the current time using the following parameters:

  • Time - Use the HH:MM or HHMM format to give a time. Use am or pm after the time to indicate a 12-hour time system. Strings like now, midnight, noon, and teatime can also be used (16:00). If the deadline has gone, the job will be completed the following day.
  • Date - You can use this command to schedule job execution for a specific date. The month name, followed by the day, and an optional year, can be used to specify the date. Strings such as today, tomorrow, or weekday can be used. The date can alternatively be expressed in the following formats: MMDD[CC]YY, MM/DD/[CC]YY, DD.MM.[CC]YY, or [CC]YY-MM-DD.
  • Increment - at also accepts increments in the now + count time-unit style, where count is a number and time-unit might be minutes, hours, days, or weeks.

Here are some instances of how time, date, and increment might be combined:

  • Schedule a job for the following Sunday at ten minutes later than it is now:
at sunday +10 minutes
  • Schedule a job for two days from now, at 1 p.m.:
at 1pm + 2 days
  • Schedule a job to start at 12:30 on Feb 21, 2022:
at 12:30 102120
  • Schedule a job that will run in one hour:
at now +1 hours

The -t option can be used to specify a time and date in the [[CC]YY]MMDDhhmm[.ss]. Here's an example:

at -t 202005111321.32

Specifying Queue

Jobs generated with at are placed in a queue by default, while jobs created with batch are placed in the b queue.

The names of queries might range from a to z and from A to Z. Lower-letter queues have a lower niceness, which means they have priority over queues with higher letters.

The -q option allows you to specify a queue. To place a job in the L queue, for example, type:

at monday +2 hours -q L

Listing Pending Jobs

Run the atq or at -l command to see a list of the user's pending jobs:

atq

All jobs will be listed one by one in the output. The work number, date, time, queue letter, and username are all listed on each line.

Output

9	  Tue May  5 12:22:00 2022 a vegastack
12	Wed Oct 21 12:30:00 2022 a vegastack
15	Tue May  5 09:00:00 2022 a vegastack
6	  Tue May  5 09:00:00 2022 a vegastack
13	Mon May  4 23:08:00 2022 a vegastack
11	Wed Jul  1 10:00:00 2022 a vegastack
4	  Tue May  5 09:00:00 2022 a vegastack

When atq is run as an administrative user, it displays a list of all users' pending jobs.

Removing Pending Jobs

Use the atrm or at -r command followed by the job number to delete a pending job. To get rid of the work with number nine, for example, run:

atrm 9

Restricting Users

You can regulate which users can create jobs using the at or batch commands using the /etc/at.deny and /etc/at.allow files. The files are made up of a list of usernames, one per line.

Only the /etc/at.deny file exists by default, and it is empty, allowing all users to use the at command. Add the username to this file if you wish to deny permission to a certain user.

Only the users mentioned in the /etc/at.allow file are allowed to use the at command if it exists.

Only users with administrator access can use the at command if none of the files exist.

FAQs on At Command in Linux

How does the at command work in Linux?

When used with a specified time, at takes the commands or script from standard input or a file and schedules its execution at the specified time.

How do I use the at command to schedule a job?

To schedule a job using at, execute the command at followed by the desired time or date in the future, and then enter the commands or specify a script to execute.

Can I specify a time format with the at command?

Yes, at supports various time formats, including absolute formats such as HH:MM, YYYY-MM-DD HH:MM, or relative formats such as now + 5 minutes.

Is it possible to schedule recurring jobs with the at command?

No, the at command is primarily used for scheduling one-time jobs. For recurring jobs, you may need to use other tools like cron.

How can I view the list of scheduled jobs with at?

To view the list of scheduled jobs, use the command atq. It will display the job ID, execution time, and the user who scheduled the job.

Can I modify or delete a scheduled job with at?

Yes, you can modify or delete a scheduled job using the atrm command followed by the job ID. This cancels the job and removes it from the queue.

Can I redirect the output of scheduled jobs with at?

Yes, you can redirect the output of scheduled jobs by using standard shell redirection. Adding > output_file to the end of the commands in the at job will redirect the output to the specified file.

Conclusion

The at utility takes commands from standard input and waits for them to be executed. Jobs created with at, unlike crontab, are only executed once.

In your terminal, type man at to learn more about all the at command's options.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.