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:
$ sudo dnf install at
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.
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:
$ man at
[Cheat sheet: Old Linux commands and their modern replacements ]
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 {runtime}
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> <EOT>
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 {runtime} {script.sh}
so that at
can read the inputs from a file instead of a standard input. Check it out:
$ rm /tmp/test.txt
$ cat << EOF > 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!
You can specify the runtime in different ways, such as:
- Time:
- HH:MM
- HHMM
- 12-hour format (am or pm)
- midnight
- noon
- teatime
- Date:
- MMDD[CC]YY
- MM/DD/[CC]YY
- DD.MM.[CC]YY
- [CC]YY-MM-DD
- today
- tomorrow
- weekday
- Increment
- now + count time-units, where the time-units can be minutes, hours, days, or weeks
The -b
parameter (or the batch
command) allows you to automatically schedule a job to run when system utilization levels are low (load average below 0.8), like this:
$ top -bc | head -1
top - 16:10:04 up 58 min, 1 user, load average: 0,00, 0,00, 0,00
$ rm /tmp/test.txt
$ ./myscript.sh | batch
warning: commands will be executed using /bin/sh
job 5 at Fri Nov 11 16:10:00 2022
$ atq
$ top -bc | head -1
top - 16:11:00 up 59 min, 1 user, load average: 0,00, 0,00, 0,00
$ cat /tmp/test.txt
It works while scripting!
Finally, to remove or cancel a pending job from the queue, you can run at -r
or atrm
and specify the job number:
$ rm /tmp/test.txt
$ date
fri nov 11 16:16:23 -03 2022
$ at 16:17 -f myscript.sh
warning: commands will be executed using /bin/sh
job 6 at Fri Nov 11 16:17:00 2022
$ atq
6 Fri Nov 11 16:17:00 2022 a localuser
$ atrm 6
$ atq
$ date
fri nov 11 16:16:59 -03 2022
$ cat /tmp/test.txt
cat: /tmp/test.txt: No such file or directory
Put your creativity to the test by imagining situations where you can take advantage of this utility. Essentially, it will be most useful for one-time job scheduling situations.
Wrap up
Knowing how to schedule tasks and jobs on your systems is very important. Some system tasks are already scheduled by default, and you need to understand how they work. In addition, you often need to automate and schedule deferred and recurring jobs to run whenever needed to accomplish programmable goals. The at
command will help you with that, and it's a fundamental utility to have in your toolkit.
I hope this and my article on the cron
utility aid you in understanding this topic and add to your general sysadmin knowledge.
저자 소개
Alexon has been working as a Senior Technical Account Manager at Red Hat since 2018, working in the Customer Success organization focusing on Infrastructure and Management, Integration and Automation, Cloud Computing, and Storage Solutions. He is a part of the TAM Practices LATAM team based in São Paulo, Brazil, where his job is partnering with, advocating, trust-advising, and supporting customers in their success goals while making use of the complete portfolio. He also contributes to produce and enhance documentation, knowledge-base articles, blog posts, presentations, webinars, and workshops. He is a member of numerous communities in addition to the Sudoers, like Red Hat Academy and Red Hat Accelerators. When he’s not at work, he enjoys spending quality time with his family (wife, daughter, and cat) and participating in several volunteer jobs.
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리
제품
- Red Hat Enterprise Linux
- Red Hat OpenShift Enterprise
- Red Hat Ansible Automation Platform
- 클라우드 서비스
- 모든 제품 보기
툴
체험, 구매 & 영업
커뮤니케이션
Red Hat 소개
Red Hat은 Linux, 클라우드, 컨테이너, 쿠버네티스 등을 포함한 글로벌 엔터프라이즈 오픈소스 솔루션 공급업체입니다. Red Hat은 코어 데이터센터에서 네트워크 엣지에 이르기까지 다양한 플랫폼과 환경에서 기업의 업무 편의성을 높여 주는 강화된 기능의 솔루션을 제공합니다.