Using at linux command line

How to schedule tasks using the Linux ‘at’ command

Man wearing a wristwatch

Time is precious, making time management an appreciated virtue in every aspect of life, whether you’re talking about financials, technology, or any other daily activity.

To manage time, a skilled sysadmin must know when and how to control tasks so that they can be programmatically executed at certain times, whether recurring or a set number of times. You can apply this concept in numerous scenarios, from scheduled backup tasks to collecting system logs periodically.

[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]

You can accomplish task scheduling in numerous ways. In this article, I focus on a straightforward tool available on Linux operating systems to help achieve this goal: the at command. My colleague Seth previously wrote a great article about at , so I recommend you check it out, as well as my article about the cron command, another Linux scheduling tool.

This article aims to be as brief, straightforward, and practical as possible, meaning I won’t be able to explore all available options for the at utility. Let’s get started!

Install the ‘at’ utility

Depending on your Linux distribution, the at utility may or may not be installed by default. You can install it using your distribution’s package manager if it’s not installed. For Red Hat Enterprise Linux (RHEL)-based distributions:

The at package installs other binaries that are used together with the main command:

$ sudo dnf repoquery -l at | grep bin [. ] /usr/bin/at /usr/bin/atq /usr/bin/atrm /usr/bin/batch /usr/sbin/atd /usr/sbin/atrun

The package provides the atd daemon, which you’ll interact with through the use of the at and atq commands:

$ sudo systemctl status atd ● atd.service - Job spooling tools Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2022-11-09 17:47:48 -03; 14min ago Main PID: 1378 (atd) Tasks: 1 (limit: 23643) Memory: 428.0K CGroup: /system.slice/atd.service └─1378 /usr/sbin/atd -f nov 09 17:47:48 demo.example.local systemd[1]: Started Job spooling tools.

Training & certification

When to use the ‘at’ utility

The at and batch ( at -b ) commands read from standard input or a specified file. The at tool allows you to specify that a command will run at a particular time. The batch command will execute commands when the system load levels drop to a specific point. Both commands use the user’s shell.

Install the at package if you need a utility for time-oriented job control. If you have a recurring job that repeats at the same time every day, week, and so forth, use crontab instead.

For more detailed information on all the possible parameters and examples of how to use at , read the man page:

Schedule tasks with the ‘at’ command

I’ll show you how at works. First, I’ll establish the time frame:

$ date fri nov 11 15:21:58 -03 2022

Now, there are some different ways to interact with the at utility. One of them is using its interactive command prompt. Do this by typing at and pressing Enter:

$ date fri nov 11 15:26:21 -03 2022 $ at 15:27 warning: commands will be executed using /bin/sh at> echo "It works!" > /tmp/test.txt at> job 2 at Fri Nov 11 15:27:00 2022

I defined a command to run at 15:27 (3:27 pm GMT-3) after defining my runtime and entering the at command prompt. In this case, the command creates a text file. To exit the command prompt, type Ctrl+D. The at utility then queues the command to execute later, according to the runtime definition. For an overview of the pending jobs for the current user, use the atq or at -l commands:

$ atq 2 Fri Nov 11 15:27:00 2022 a localuser

From the previous output, you can see the following:

  • 2 is the unique job number.
  • Fri Nov 11 15:27:00 2022 is the execution date and time for the scheduled job.
  • a indicates that the job is scheduled with the default queue a.
  • localuser is the job owner (and the user the job runs as).
Читайте также:  Common directory in linux

[ Want to test your sysadmin skills? Take a skills assessment today. ]

After waiting for 39 seconds, here’s what happens when the job gets out of the queue and executes:

$ date fri nov 11 15:27:01 -03 2022 $ atq $ cat /tmp/test.txt It works!

Of course, this is not an ideal way of running scheduled jobs; another way is by pipelining the desired command as an input for the at utility. Learn more about that in my article about manipulating files with shell redirection and pipelines. Here is a quick example:

$ rm /tmp/test.txt $ echo "It works while pipelining!" > /tmp/test.txt | at 15:41 warning: commands will be executed using /bin/sh job 3 at Fri Nov 11 15:41:00 2022 $ atq 3 Fri Nov 11 15:41:00 2022 a localuser $ date fri nov 11 15:40:40 -03 2022 $ atq $ date fri nov 11 15:41:01 -03 2022 $ cat /tmp/test.txt It works while pipelining!

Use flags with the ‘at’ command

The most used form of the at utility is specifying an existing script with the -f parameter (or using shell redirection like at so that at can read the inputs from a file instead of a standard input. Check it out:

$ rm /tmp/test.txt $ cat myscript.sh > echo "It works while scripting!" > /tmp/test.txt > EOF $ ls myscript.sh $ sudo chmod +x myscript.sh $ date fri nov 11 15:50:58 -03 2022 $ at 15:52 -f ./myscript.sh warning: commands will be executed using /bin/sh job 4 at Fri Nov 11 15:52:00 2022 $ atq 4 Fri Nov 11 15:52:00 2022 a localuser $ date fri nov 11 15:52:00 -03 2022 $ atq $ cat /tmp/test.txt It works while scripting!

Источник

At Command in Linux

at is a command-line utility that allows you to schedule commands to be executed at a particular time. Jobs created with at are executed only once.

In this article, we will explain how to use at and its companion utilities batch , atq , atrm to view, delete, and create jobs to be executed at a later time.

Читайте также:  Nvidia 340 arch linux

Installing at #

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

If at is not installed, you can easily install it using the package manager of your distribution.

    Install at on Ubuntu and Debian

sudo apt update sudo apt install at

Once the program is installed make sure atd , the scheduling daemon is running and set to start on boot:

sudo systemctl enable --now atd

How to Use the at Command #

The simplified syntax for the at command is as follows:

The at command takes the date and time ( runtime ) when you want to execute the job as a command-line parameter, and the command to be executed from the standard input.

Let’s create a job that will be executed at 9:00 am:

Once you hit Enter , you’ll be presented with the at command prompt that most often starts with at> . You also see a warning that tells you the shell in which the command will run:

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

Enter one or more command you want to execute:

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

When you’re done entering the commands, press Ctrl-D to exit the prompt and save the job:

at> job 4 at Tue May 5 09:00:00 2020 

The command will display the job number and the execution time and date.

There are also other ways to pass the command you want to run, besides entering the command in the at prompt. One way is to use echo and pipe the command to at :

echo "command_to_be_run" | at 09:00
at 09:00 command_to_be_runEND

To read the commands from a file instead of the standard input, invoke the command with -f option following by the path to the file. For example, to create a job that will run the script /home/linuxize/script.sh :

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

By default if the command produces output, at will send an email including the output to the user once the job is completed. Invoke at with the -M option to suppress the email notification:

Use the -m to send an email even if there is no output:

batch Command #

batch or its alias at -b schedules jobs and executes them in a batch queue when the system load level permit. By default, the jobs are executed when the system load average is below 1.5. The value of the load can be specified when invoking the atd daemon. If the system load average is higher the specified one, the jobs will wait in the queue.

To create a job with batch , pass the commands you want to execute:

echo "command_to_be_run" | batch

Specifying the Execution Time #

The at utility accepts a wide range of time specifications. You can specify time, date, and increment from the current time:

  • Time - To specify a time, use the HH:MM or HHMM form. To indicate a 12-hour time format, use am or pm after the time. You can also use strings like now , midnight , noon , or teatime (16:00). If the specified time is passed, the job will be executed the next day.
  • Date - The command allows you to schedule job execution on a given date. The date can be specified using the month name followed by the day and an optional year. You can use strings, such as today , tomorrow , or weekday. The date can be also indicated using the MMDD[CC]YY , MM/DD/[CC]YY , DD.MM.[CC]YY or [CC]YY-MM-DD formats.
  • Increment - at also accepts increments in the now + count time-unit format, where count is a number and time-unit can be one of the following strings: minutes , hours , days , or weeks .

Time, date and increment can be combined, here are few examples:

    Schedule a job for the coming Sunday at a time ten minutes later than the current time:

You can also specify a time and date in the [[CC]YY]MMDDhhmm[.ss] using the -t option. Here is an example:

Specifying Queue #

By default, the jobs created with at are scheduled in the queue named a and jobs created with batch are scheduled in the b queue.

Queries can have a name from a to z and A to Z . Queues with lower letters run with lower niceness, which means they have priority over those with higher letters.

You can specify the queue with the -q option. For example, to set a job in the L queue, you would run:

Listing Pending Jobs #

To list the user’s pending jobs run the atq or at -l command:

The output will list all jobs, one per line. Each line includes the job number, date, time, queue letter, and username.

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

When atq is invoked as an administrative user, it will list the pending jobs of all users.

Removing Pending Jobs #

To remove a pending job invoke the atrm or at -r command followed by the job number. For example, to remove the job with number nine, you would run:

Restricting Users #

The /etc/at.deny and /etc/at.allow files allow you to control which users can create jobs with at or batch command. The files consist of a list of usernames, one user name per line.

By default, only the /etc/at.deny file exists and is empty, which means that all users can use the at command. If you want to deny permission to a specific user, add the username to this file.

If the /etc/at.allow file exists only the users who are listed in this file can use the at command.

If neither of the files exists, only the users with administrative privileges can use the at command.

Conclusion #

The at utility reads commands from standard input and executes them at a later time. Unlike crontab , jobs created with at are executed only once.

For more information about all available options of the at command type man at in your terminal.

If you have any questions, feel free to leave a comment.

Источник

Оцените статью
Adblock
detector