Detailed explanation of using the at command for one-time scheduled tasks in Linux

Detailed explanation of using the at command for one-time scheduled tasks in Linux

Preface

A planned task is a task with a plan, which means we want to execute the task automatically according to our plan.

There are 2 key points :

The first is scheduled, the second is automatic.

1. Introduction to one-time planned tasks

A one-time planned task is a task that is executed only once according to the plan.

2. Command

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

By default, the atd service checks the directory every 60 seconds. When there is a "schedule", it checks the "schedule" running time. If the "schedule running time" matches the current time, it runs this "schedule".

3. Create a one-time scheduled task

First, the at service must be turned on:

systemctl start atd

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

systemctl status atd

Check whether it is enabled:

systemctl is-active atd

Stop the atd service (one-time scheduled tasks will no longer be executed):

systemctl stop atd

[root@localhost ~]# vim /root/backup-yum-repo.sh
[root@localhost ~]# cat /root/backup-yum-repo.sh 
#!/bin/bash
mkdir /opt/yum-repo-backup-dir -p
cp -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 a one-time backup of the yum repository file. In order to smoothly 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 to set the date and time [root@localhost ~]# date -s "2021-12-1 16:58:30"

Example 1: One-time scheduled task at a specific time

Execute at 5:00 p.m., that is, 17:00 (if the time is past the afternoon, then it will be postponed to the next day)

# The <EOT> in the following command is the scheduled task submitted to at. Use ctrl+d to display [root@localhost ~]# at 5:00PM
warning: commands will be executed using /bin/sh
at> /root/backup-yum-repo.sh
at> <EOT>
job 5 at Wed Dec 1 17:00:00 2021
# atq View one-time scheduled tasks [root@localhost ~]# atq
5 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 2021
5 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 2021
yum.repos.d-2021-12-01-17:00:00.bak

Example 2: One-time scheduled task on a specific date

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

If time is not specified, the time of the custom scheduled task is used as the execution time. (If the specified date is today, the scheduled task will be executed in the next minute)

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

Example 3: One-time scheduled task at a specific date and time

Executed at 12:12 on December 13, 2021

[root@localhost ~]# at 12:12 2021-12-13
warning: commands will be executed using /bin/sh
at> /root/backup-yum-repo.sh
at> <EOT>
job 7 at Mon Dec 13 12:12:00 2021
[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root

Example 4: Execute at 9:15 am 5 days later

[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root
[root@localhost ~]# at 9:15 + 5 days
warning: commands will be executed using /bin/sh
at> /root/backup-yum-repo.sh
at> <EOT>
job 8 at Mon Dec 6 09:15:00 2021
[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root
8 Mon Dec 6 09:15:00 2021 a root

Example 5: Execute on Monday

[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root
8 Mon Dec 6 09:15:00 2021 a root
[root@localhost ~]# at monday
warning: commands will be executed using /bin/sh
at> /root/backup-yum-repo.sh            
at> <EOT>
job 9 at Mon Dec 6 17:18:00 2021
[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root
8 Mon Dec 6 09:15:00 2021 a root
9 Mon Dec 6 17:18:00 2021 a root

Note: You cannot use at with past time. There is no regret medicine.

4. One-time planned task management

4.1 View one-time scheduled tasks

atq

[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root
8 Mon Dec 6 09:15:00 2021 a root
9 Mon Dec 6 17:18:00 2021 a root

4.2 Deleting a one-time scheduled task

atrm

[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root
8 Mon Dec 6 09:15:00 2021 a root
9 Mon Dec 6 17:18:00 2021 a root
[root@localhost ~]# atrm 8
[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root
9 Mon Dec 6 17:18:00 2021 a root

4.3 View detailed information of one-time scheduled tasks

at -c job_id

[root@localhost ~]# atq
6 Sun Dec 12 17:07:00 2021 a root
7 Mon Dec 13 12:12:00 2021 a root
9 Mon Dec 6 17:18:00 2021 a root
[root@localhost ~]# at -c 7

5. One-time scheduled task usage control

at.allow (/etc/at.allow)

at.deny (/etc/at.deny)

Users can use the at command to set a one-time scheduled task, and can also control which users can use the scheduled task (in the whitelist) and which users cannot use the scheduled task

The whitelist for at one-time scheduled tasks is at.allow and the blacklist is at.deny. It is not recommended to use both whitelist and blacklist.

It is recommended to use a whitelist, and at.allow has a higher priority than at.deny. That is, if wang is in both the whitelist and the blacklist, then wang can execute at

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

# Add user wang to the blacklist [root@localhost ~]# cat /etc/at.deny 
wang
[root@localhost ~]# su - wang
[wang@localhost ~]$ at
You do not have permission to use at.
# Add user wang to both the blacklist and the whitelist [root@localhost ~]# cat /etc/at.allow
wang
[root@localhost ~]# cat /etc/at.deny 
wang
[root@localhost ~]# su - wang
[wang@localhost ~]$ at
Garbled time

This concludes this article on the detailed use of the Linux one-time scheduled task at command. For more relevant Linux one-time scheduled tasks, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to customize at and cron scheduled tasks in Linux
  • Detailed explanation of how to use cron scheduled tasks in Linux
  • Brief discussion: Summary of commonly used symbols for Linux cron scheduled tasks
  • Introduction to scheduled tasks in Linux system

<<:  Getting Started with Front-End Vue Unit Testing

>>:  A brief analysis of the issues that should be paid attention to when making 404 error pages

Recommend

In-depth analysis of MySQL index data structure

Table of contents Overview Index data structure B...

Detailed process of installing Jenkins-2.249.3-1.1 with Docker

Table of contents 1. Install Docker 2. Pull the J...

Implementation of Mysql User Rights Management

1. Introduction to MySQL permissions There are 4 ...

Nginx forwarding based on URL parameters

Use scenarios: The jump path needs to be dynamica...

Vue SPA first screen optimization solution

Table of contents Preface optimization SSR Import...

Automatically load kernel module overlayfs operation at CentOS startup

To automatically load kernel modules in CentOS, y...

Mysql anonymous login cannot create a database problem solution

Frequently asked questions Access denied for user...

Linux traceroute command usage detailed explanation

Traceroute allows us to know the path that inform...

Introduction to the use of HTML element noscript

noscript definition and usage The noscript elemen...

Detailed explanation of MySQL Explain

In daily work, we sometimes run slow queries to r...

Detailed explanation of hosts file configuration on Linux server

Linux server hosts file configuration The hosts f...

Detailed explanation of using Vue.prototype in Vue

Table of contents 1. Basic Example 2. Set the sco...

Each time Docker starts a container, the IP and hosts specified operations

Preface Every time you use Docker to start a Hado...