Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Linux one-time schedule task at command

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "Linux one-time planning task at command how to use", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Linux one-time planning task at command how to use" this article.

Preface

A planned task is a planned task, which means that we want to carry out the task automatically according to our plan.

There are 2 key points:

The first is a plan, and the second is automatic.

1. Introduction to one-time scheduled tasks

The so-called one-time scheduled tasks are tasks that are only carried out once according to the plan.

two。 Command

The at command in Linux is used to create an one-time scheduled task, and the at command has a service atd that runs in background mode, checking the current time to decide whether to run the schedule.

By default, the atd service checks the directory every 60 seconds, checks the scheduled run time when there is a schedule, and runs the schedule if the scheduled run time matches the current time.

3. Create an one-time scheduled task

First, the at service must be open:

Systemctl start atd

Check the status of atd (whether it is active):

Systemctl status atd

Check to see if it is on:

Systemctl is-active atd

Stop the atd service (you can no longer perform one-time scheduled tasks):

Systemctl stop atd

[root@localhost ~] # vim / root/backup-yum-repo.sh [root@localhost ~] # cat / root/backup-yum-repo.sh #! / bin/bashmkdir / opt/yum-repo-backup-dir-pcp-r / etc/yum.repos.d / opt/yum-repo-backup-dir/yum.repos.d- `date + "% Y-%m-%d-%H:%M:%S" `.bak [root@localhost ~] # chmod + x backup-yum-repo.sh

Use the at command to perform an one-time backup of yum repository files. In order to successfully view the execution results of at, use the following command to turn off ntp synchronization and set the current time

# timedatectl set-ntp 0 command turns off ntp synchronization [root@localhost ~] # timedatectl set-ntp 0#date-s "2021-12-1 16:58:30" command sets date and time [root@localhost ~] # date-s "2021-12-1 16:58:30"

Example 1: an one-time scheduled task at a specific time

Execution at 5 o'clock in the afternoon, that is, execution at 17:00 (if the time at this time has already passed the afternoon of the day, then, postpone to the next day)

# the scheduled tasks submitted to at are in the following command. Using ctrl+d, [root@localhost ~] # at 5:00PMwarning: commands will be executed using / bin/shat > / root/backup-yum-repo.shat > job 5 at Wed Dec 1 17:00:00 202 scheduled atq view one-time scheduled tasks [root@localhost ~] # atq5 Wed Dec 1 17:00:00 2021 a root [root@localhost ~] # date;atq Ls / opt/yum-repo-backup-dir/Wed Dec 1 16:59:50 CST 20215 Wed Dec 1 17:00:00 2021 a root [root@localhost ~] # date;atq;ls / opt/yum-repo-backup-dir/Wed Dec 1 17:00:02 CST 2021yum.repos.d-2021-12-01-17:00:00.bak

Example 2: one-time scheduled tasks with specific dates

Execute a script at this moment on December 12, 2021.

If no time is specified, then the time of the scheduled task is customized as the time of the execution day. (if the specified date is the same day, the scheduled task will be executed the next minute)

[root@localhost] # at 2021-12-12warning: commands will be executed using / bin/shat > / root/backup-yum-repo.shat > job 6 at Sun Dec 12 17:07:00 2021 [root@localhost] # atq6 Sun Dec 12 17:07:00 2021 a root

Example 3: one-time scheduled tasks with specific dates and times

Executed at 12:12 on December 13th, 2021

[root@localhost] # at 12:12 2021-12-13warning: commands will be executed using / bin/shat > / root/backup-yum-repo.shat > job 7 at Mon Dec 13 12:12:00 2021 [root@localhost] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root

Example 4: executed at 09:15 five days later

[root@localhost ~] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root [root@localhost] # at 9:15 + 5 dayswarning: commands will be executed using / bin/shat > / root/backup-yum-repo.shat > job 8 at Mon Dec 6 09:15:00 2021 [root@localhost ~] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root8 Mon Dec 6 09:15:00 2021 a root

Example 5: execute on Monday

[root@localhost ~] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root8 Mon Dec 6 09:15:00 2021 a root [root@localhost] # at mondaywarning: commands will be executed using / bin/shat > / root/backup-yum-repo.sh at > job 9 at Mon Dec 6 17:18:00 2021 [root@localhost ~] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root8 Mon Dec 6 09:15:00 2021 a root9 Mon Dec 6 17:18:00 2021 a root

Note: you can't use at in the past, there is no regret medicine.

4. One-time scheduled task management 4.1 View one-time scheduled tasks

Atq

[root@localhost ~] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root8 Mon Dec 6 09:15:00 2021 a root9 Mon Dec 6 17:18:00 2021 a root4.2 Delete one-time scheduled tasks

Atrm

[root@localhost] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root8 Mon Dec 6 09:15:00 2021 a root9 Mon Dec 6 17:18:00 2021 a root [root@localhost] # atrm 8 [root@localhost ~] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root9 Mon Dec 6 17:18:00 2021 a root4.3 to view the details of one-time scheduled tasks

At-c job_id

[root@localhost] # atq6 Sun Dec 12 17:07:00 2021 a root7 Mon Dec 13 12:12:00 2021 a root9 Mon Dec 6 17:18:00 2021 a root [root@localhost] # at-c 75. One-time scheduled task usage control

At.allow (/ etc/at.allow)

At.deny (/ etc/at.deny)

Users can use the at command to set up one-time scheduled tasks, and they can also control which users can use scheduled tasks (in the whitelist) and which users cannot use scheduled tasks.

The whitelist of at's one-time scheduled tasks is at.allow and the blacklist is at.deny. It is not recommended to use both whitelists and blacklists.

Whitelist is recommended. At.allow takes precedence over at.deny. That is, if wang is on both the whitelist and blacklist, wang can execute at.

By default, there is no at.allow file in the rhel8.0 system, so you can create it yourself.

# add user wang to blacklist [root@localhost ~] # cat / etc/at.deny wang [root@localhost ~] # su-wang [wang@localhost ~] $atYou do not have permission to use at.# add user wang to blacklist Add to the whitelist [root@localhost ~] # cat / etc/at.allowwang [root@localhost ~] # cat / etc/at.deny wang [root@localhost ~] # su-wang [wang@localhost ~] $atGarbled time all the contents of the article "how to use the Linux one-time scheduled task at command". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 223

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report