Linux|Strange knowledge|Use of the at command for one-time tasks

Foreword:

The at command is a proprietary command of Linux. This command is the client command of the old scheduled task atd service (the at command is the client in the software suite in the form of c/s). The main purpose is to flexibly formulate a job Plan to automatically complete the tasks you set at a specific time.

Well, some veterans may say that the at command and crontab seem to have overlapping functions, why do you need to know this command?

Indeed, crontab is undoubtedly more powerful as an old-fashioned scheduled task, but the at command is more flexible than crontab, and the time range of the scheduled task may have a relatively wide setting, which can use midnight (late night) and noon (noon ) , teatime (tea drinking time, usually 4 pm) and other vague, but very colloquial words to specify the time . Therefore, some application scenarios of crontab will be filled, but at can only perform tasks once, for example, some logs need to be cleaned up every day, at cannot be executed repeatedly, but crontab can perform repetitive periodic tasks task.

The following will give a simple explanation on how to use the at command to make a reliable scheduled task.

one,

The form of the at command

The at command is the client of the atd service, and the atd service is a daemon process.

On operating systems such as centos7, there is no at command for minimal installation, and it needs to be installed separately. The installation command is:

yum install at -y

After the installation is complete, you need to start the service, the command is:

systemctl enable atd && systemctl start atd

 

The status of the atd service 

[root@centos61 ~]# systemctl status atd
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2023-05-28 11:29:17 CST; 18min ago
 Main PID: 666 (atd)
   Memory: 260.0K
   CGroup: /system.slice/atd.service
           └─666 /usr/sbin/atd -f

May 28 11:29:17 centos61 systemd[1]: Started Job spooling tools.
May 28 11:29:17 centos61 systemd[1]: Starting Job spooling tools...

two,

Three Elements of Task Planning

In fact, planned tasks can also be written according to the three elements of the event, that is, time, place, and event. Then, specifically in the Linux system, there is no doubt that the time refers to the time when the task starts, the location is naturally the local machine, and the event is the task.

Then, relative to the at command, the time setting is extremely rich. For example, after 10 minutes relative to the current time, today (today), tomorrow (tomorrow), midnight (late night), noon (noon), teatime (drink Tea time, usually at 4 pm)  , minutes (minutes), hours (hours), days (days), weeks (weeks) and other vague words to specify the time, the user can also use the 12-hour timer, that is, the Add AM (morning) or PM (afternoon) to the end to indicate whether it is morning or afternoon.

Generally speaking, the time setting of the at command is closer to people's usual concept of time, that is to say, it can be very colloquial.

There are also many tasks supported by the at command, such as shell scripts, commands, combined commands, etc.

After the task programmed by at is executed, it will send a local email to itself. For example, if a task is executed as the root user, then after the task is completed, the root user will receive an email. The email storage location is usually /var/ spool/mail/root

three,

at task example

It needs to be explained that the usual operation mode of the at command is the interactive mode, that is, first specify the time, then write several tasks to be executed, and finally exit.

1,

After a minute, view the contents of the /etc/passwd file

[root@centos61 ~]# date
Sun May 28 13:25:03 CST 2023

[root@centos61 ~]# at now +1 minute
at> cat /etc/passwd
at> exit
at> quit
at> <EOT>
job 11 at Sun May 28 13:26:00 2023

After waiting for 1 minute, check the email, and you can see that the task has indeed been executed:

Subject: Output from your job       11
To: [email protected]
Message-Id: <[email protected]>
Date: Sun, 28 May 2023 13:26:00 +0800 (CST)
From: [email protected] (root)

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
postgres:x:1000:1000::/home/postgres:/bin/bash
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

2,

Three minutes later, touch a file named fuck in the root directory

[root@centos61 ~]# at now +3 minute
at> touch /root/fuck
at> <EOT>
job 12 at Sun May 28 13:31:00 2023
[root@centos61 ~]# atq 
12	Sun May 28 13:31:00 2023 a root

3,

Run the df command at this time tomorrow, you can see that the task number is 14

[root@centos61 ~]# at tomorrow
at> df
at> <EOT>
job 14 at Mon May 29 13:38:00 2023
[root@centos61 ~]# atq
14	Mon May 29 13:38:00 2023 a root

View task No. 14 (the specific task is at the end, the front is full of environment variables, nothing to see):

[root@centos61 ~]# at -c 14
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
XDG_SESSION_ID=1; export XDG_SESSION_ID
HOSTNAME=centos61; export HOSTNAME
SELINUX_ROLE_REQUESTED=; export SELINUX_ROLE_REQUESTED
SHELL=/bin/bash; export SHELL
HISTSIZE=1000; export HISTSIZE
SSH_CLIENT=192.168.123.1\ 49853\ 22; export SSH_CLIENT
SELINUX_USE_CURRENT_RANGE=; export SELINUX_USE_CURRENT_RANGE
SSH_TTY=/dev/pts/0; export SSH_TTY
USER=root; export USER
LS_COLORS=rs=0:di=01\;34:ln=01\;36:mh=00:pi=40\;33:so=01\;35:do=01\;35:bd=40\;33\;01:cd=40\;33\;01:or=40\;31\;01:mi=01\;05\;37\;41:su=37\;41:sg=30\;43:ca=30\;41:tw=30\;42:ow=34\;42:st=37\;44:ex=01\;32:\*.tar=01\;31:\*.tgz=01\;31:\*.arc=01\;31:\*.arj=01\;31:\*.taz=01\;31:\*.lha=01\;31:\*.lz4=01\;31:\*.lzh=01\;31:\*.lzma=01\;31:\*.tlz=01\;31:\*.txz=01\;31:\*.tzo=01\;31:\*.t7z=01\;31:\*.zip=01\;31:\*.z=01\;31:\*.Z=01\;31:\*.dz=01\;31:\*.gz=01\;31:\*.lrz=01\;31:\*.lz=01\;31:\*.lzo=01\;31:\*.xz=01\;31:\*.bz2=01\;31:\*.bz=01\;31:\*.tbz=01\;31:\*.tbz2=01\;31:\*.tz=01\;31:\*.deb=01\;31:\*.rpm=01\;31:\*.jar=01\;31:\*.war=01\;31:\*.ear=01\;31:\*.sar=01\;31:\*.rar=01\;31:\*.alz=01\;31:\*.ace=01\;31:\*.zoo=01\;31:\*.cpio=01\;31:\*.7z=01\;31:\*.rz=01\;31:\*.cab=01\;31:\*.jpg=01\;35:\*.jpeg=01\;35:\*.gif=01\;35:\*.bmp=01\;35:\*.pbm=01\;35:\*.pgm=01\;35:\*.ppm=01\;35:\*.tga=01\;35:\*.xbm=01\;35:\*.xpm=01\;35:\*.tif=01\;35:\*.tiff=01\;35:\*.png=01\;35:\*.svg=01\;35:\*.svgz=01\;35:\*.mng=01\;35:\*.pcx=01\;35:\*.mov=01\;35:\*.mpg=01\;35:\*.mpeg=01\;35:\*.m2v=01\;35:\*.mkv=01\;35:\*.webm=01\;35:\*.ogm=01\;35:\*.mp4=01\;35:\*.m4v=01\;35:\*.mp4v=01\;35:\*.vob=01\;35:\*.qt=01\;35:\*.nuv=01\;35:\*.wmv=01\;35:\*.asf=01\;35:\*.rm=01\;35:\*.rmvb=01\;35:\*.flc=01\;35:\*.avi=01\;35:\*.fli=01\;35:\*.flv=01\;35:\*.gl=01\;35:\*.dl=01\;35:\*.xcf=01\;35:\*.xwd=01\;35:\*.yuv=01\;35:\*.cgm=01\;35:\*.emf=01\;35:\*.axv=01\;35:\*.anx=01\;35:\*.ogv=01\;35:\*.ogx=01\;35:\*.aac=01\;36:\*.au=01\;36:\*.flac=01\;36:\*.mid=01\;36:\*.midi=01\;36:\*.mka=01\;36:\*.mp3=01\;36:\*.mpc=01\;36:\*.ogg=01\;36:\*.ra=01\;36:\*.wav=01\;36:\*.axa=01\;36:\*.oga=01\;36:\*.spx=01\;36:\*.xspf=01\;36:; export LS_COLORS
MAIL=/var/spool/mail/root; export MAIL
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/pgsql/bin:/opt/pgsql/data:/root/bin; export PATH
PWD=/root; export PWD
LANG=en_US.UTF-8; export LANG
SELINUX_LEVEL_REQUESTED=; export SELINUX_LEVEL_REQUESTED
PGHOME=/opt/pgsql; export PGHOME
HISTCONTROL=ignoredups; export HISTCONTROL
SHLVL=1; export SHLVL
HOME=/root; export HOME
LOGNAME=root; export LOGNAME
SSH_CONNECTION=192.168.123.1\ 49853\ 192.168.123.61\ 22; export SSH_CONNECTION
PGDATA=/opt/pgsql/data; export PGDATA
LESSOPEN=\|\|/usr/bin/lesspipe.sh\ %s; export LESSOPEN
XDG_RUNTIME_DIR=/run/user/0; export XDG_RUNTIME_DIR
cd /root || {
	 echo 'Execution directory inaccessible' >&2
	 exit 1
}
${SHELL:-/bin/sh} << 'marcinDELIMITER5f46f2f8'
df

marcinDELIMITER5f46f2f8

4,

Specify a certain time of the day to execute ls /opt/

[root@centos61 ~]# at 14:51
at> ls /opt  
at> <EOT>
job 16 at Sun May 28 14:51:00 2023

5,

Specify a specific time to execute the task:

It's clear that it's 2023 and 2020 is not an acceptable time

[root@centos61 ~]# at 19:19 2020-05-13
at: refusing to create job destined in the past

Future times are acceptable:

[root@centos61 ~]# at 19:19 2023-05-31
at> <EOT>
job 17 at Wed May 31 19:19:00 2023

6,

Midnight task (midnight here refers to zero o'clock, today is Sunday, and the start time of the task is Monday 7)

[root@centos61 ~]# at midnight
at> ls /opt
at> <EOT>
job 18 at Mon May 29 00:00:00 2023

7,

Weekly tasks (Wednesday, tasks executed at the current time)

root@centos61 ~]# at Wednesday
at> <EOT>
job 19 at Wed May 31 14:59:00 2023
[root@centos61 ~]# date
Sun May 28 14:59:54 CST 2023

8,

Tasks for tomorrow at 2:33pm:

[root@centos61 ~]# at 02:33PM tomorrow
at> <EOT>
job 20 at Mon May 29 14:33:00 2023

9,

Execute the script to start postgresql after one minute:

[root@centos61 ~]# at -f start-pg.sh now +1 minute
job 22 at Sun May 28 16:03:00 2023

Check whether the script is executed (one more thing, the script does not need execution permission, because the at command calls /bin/sh):

[root@centos61 ~]# ls -l start-pg.sh 
-rw-r--r--. 1 root root 52 May 20 17:34 start-pg.sh

[root@centos61 ~]# tail  /var/spool/mail/root 

waiting for server to start....2023-05-28 16:03:00.791 CST [21108] LOG:  starting PostgreSQL 12.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
2023-05-28 16:03:00.795 CST [21108] LOG:  listening on IPv4 address "0.0.0.0", port 35432
2023-05-28 16:03:00.795 CST [21108] LOG:  listening on IPv6 address "::", port 35432
2023-05-28 16:03:00.805 CST [21108] LOG:  listening on Unix socket "/tmp/.s.PGSQL.35432"
2023-05-28 16:03:00.863 CST [21109] LOG:  database system was shut down at 2023-05-28 00:00:17 CST
2023-05-28 16:03:00.890 CST [21108] LOG:  database system is ready to accept connections
 done
server started

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/130910905