Linux at command reference

at command in Linux with Examples

at and batch command read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh. at command executes commands at a specified time.

batch executes commands when system load levels permit; in other words, when the load average drops below 1.5, or the value specified in the invocation of atd command.

at allows fairly complex time specifications, extending the POSIX.2 standard. It accepts times of the form HH:MM to run a job at a specific time of day. (If that time is already past, the next day is assumed.) You may also specify midnight, noon, or teatime (4pm) and you can have a time-of-day suffixed with AM or PM for running in the morning or the evening. You can also say what day the job will be run, by giving a date in the form month-name day with an optional year, or giving a date of the form MMDD[CC]YY, MM/DD/[CC]YY, DD.MM.[CC]YY or [CC]YY-MM-DD. The specification of a date must follow the specification of the time of day. You can also give times like now + count time-units, where the time-units can be minutes, hours, days, or weeks and you can tell at to run the job today by suffixing the time with today and to run the job tomorrow by suffixing the time with tomorrow.

For example, to run a job at 4pm three days from now, you would do at at 4pm + 3 days , to run a job at 10:00am on July 31, you would do at 10am Jul 31 and to run a job at 1am tomorrow, you would do run at 1am tomorrow command.

If you specify a job to absolutely run at a specific time and date in the past, the job will run as soon as possible. For example, if it is 8pm and you do a at 6pm today, it will run more likely at 8:05pm.

For both at and batch , commands are read from standard input or the file specified with the -f option and then executed.

Options

The options for at commands are:

-V prints the version number to standard error and exit successfully. -q queue uses the specified queue. A queue designation consists of a single letter; valid queue designations range from a to z and A to Z. The a queue is the default for at and the b queue for batch. Queues with higher letters run with increased niceness. The special queue "=" is reserved for jobs which are currently running. -m Send mail to the user when the job has completed even if there was no output. -M Never send mail to the user. -f file Reads the job from file rather than standard input. -t time run the job at time, given in the format [[CC]YY]MMDDhhmm[.ss] -l Is an alias for atq. -r Is an alias for atrm. -d Is an alias for atrm. -b is an alias for batch. -v Shows the time the job will be executed before reading the job. Times displayed will be in the format "Thu Feb 20 14:50:00 1997". -c cats the jobs listed on the command line to standard output.

Examples

1. at command without any arguments allows jobs to be executed once in the future at a predefined date or time without editing any configuration file.

$ ping -c 5 google.com > ping.log | at now + 1min warning: commands will be executed using /bin/sh job 5 at Mon Jun 7 15:31:00 2021

We can see that the command has been queued, and its job id is 5, it also shows the time at which it would be executed.

Читайте также:  Собираем свою сборку linux

We can see the ouput of the executed at command below.

$ cat ping.log PING google.com (142.250.77.206) 56(84) bytes of data. 64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=1 ttl=117 time=3.77 ms 64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=2 ttl=117 time=2.98 ms 64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=3 ttl=117 time=3.02 ms 64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=4 ttl=117 time=2.98 ms 64 bytes from del11s08-in-f14.1e100.net (142.250.77.206): icmp_seq=5 ttl=117 time=3.14 ms --- google.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4003ms rtt min/avg/max/mdev = 2.985/3.184/3.776/0.303 ms

2. We can also use at command to read commands from standard input, which would be executed at a later time, using ‘/bin/sh’.

$ at 15:55 warning: commands will be executed using /bin/sh at> ping -c 3 ebhashasetu.com > ebs.log at> job 10 at Mon Jun 7 15:55:00 2021 $ atq 10 Mon Jun 7 15:55:00 2021 a expert $ cat ebs.log PING ebhashasetu.com (166.62.28.105) 56(84) bytes of data. 64 bytes from ip-166-62-28-105.ip.secureserver.net (166.62.28.105): icmp_seq=1 ttl=51 time=80.7 ms 64 bytes from ip-166-62-28-105.ip.secureserver.net (166.62.28.105): icmp_seq=2 ttl=51 time=77.0 ms 64 bytes from ip-166-62-28-105.ip.secureserver.net (166.62.28.105): icmp_seq=3 ttl=51 time=77.2 ms --- ebhashasetu.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2005ms rtt min/avg/max/mdev = 77.050/78.351/80.786/1.738 ms

3. We can see the list of jobs in the queue using atq command or use option -l which is an alias for atq command.

$ atq 3 Mon Jun 7 15:26:00 2021 = expert 4 Mon Jun 7 15:27:00 2021 = expert 6 Mon Jun 7 15:35:00 2021 a expert
$ at -l 3 Mon Jun 7 15:26:00 2021 = expert 4 Mon Jun 7 15:27:00 2021 = expert

We can observe, as the time elapsed, one of the job is executed and there is one job less in the queue.

4. We can use atrm command to remove a job from the queue, or we can use option -r or -d which is an alias for atrm command.

$ atq 3 Mon Jun 7 15:26:00 2021 = expert 4 Mon Jun 7 15:27:00 2021 = expert $ atrm 3 Warning: deleting running job $ atq 4 Mon Jun 7 15:27:00 2021 = expert

5. We can use -f option to read the jobs from the file rather than standard input.

$ at now +1 min -f cmd.sh warning: commands will be executed using /bin/sh job 15 at Mon Jun 7 16:25:00 2021 $ atq 15 Mon Jun 7 16:25:00 2021 a expert $ ls -l all-sample.tgz -rw-rw-r-- 1 expert expert 181544 Jun 7 16:25 all-sample.tgz $ cat cmd.log sample-file-3.txt sample-file.txt sample.txt sample1.txt sample3.txt sample4.txt

Источник

Читайте также:  Как пробросить ssh туннель linux

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).

[ 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!

Источник

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