Linux的例行性工作

1、单一的例行性工作:仅处理执行一次就结束了,at -> atd

我们使用 at 命令来生成所要运行的工作,并将这个工作,以文本方式写入/var/spool/at/ 目录内,该 工作便能等待 atd 这个服务的取用与执行了。当然,为了安全问题,不是所有人都可以进行 at 工作调度。我们可以利用/etc/at.allow 和/etc/at.deny 这两个文件来进行 at 的使用限制。

at命令的实际工作过程

1、寻找到/etc/at.allow 文件存在,写在这个文件的用户才能使用at命令
2、/etc/at.allow 不存在,则寻找/etc/at.deny 文件,写在该文件的用户不能使用at命令
3、若两个文件都不存在,那么只有root可以使用at命令

注意:在 rhel 里面,由于假设系统上的所有用户都是可信任的,所以允许所有人使用 at 命
令。/etc/at.deny 文件为空,如果你想设置拒绝某人使用 at 命令,你可将该用户写入/etc/at.deny 文件。其次,atd在寻找文件的时候,会先寻找allow文件中的用户,找到之后即使你在deny中将该用户写入文件也不会让该用户无法使用at命令

[root@server ~]# systemctl status atd
● atd.service - Job spooling toolsLoaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)Active: active (running) since Fri 2022-11-04 06:55:34 EDT; 2h 2min agoMain PID: 1323 (atd)Tasks: 1 (limit: 4652)Memory: 1.0MCGroup: /system.slice/atd.service└─1323 /usr/sbin/atd -fNov 04 06:55:34 server systemd[1]: Started Job spooling tools.
Nov 04 08:46:00 server atd[4058]: Starting job 1 (a0000101a8157e) for user 'root' (0)
Nov 04 08:46:00 server atd[4058]: pam_unix(atd:session): session opened for user root by >
Nov 04 08:46:00 server atd[4058]: pam_unix(atd:session): session closed for user root
lines 1-13/13 (END)

注意,在写完你所需要at执行的命令后需要用ctrl+d来提交。

at命令详解 
命令格式:at [参数] [时间]

参数 说明
-m 当任务完成之后,即使没有标准输出,将给用户发送邮件
-l atq的别名,可以列出目前系统上面的所有该用户的at调度
-d

atrm别名,可以取消一个在at调度中的工作

-v

使用较为明显的时间格式,列出at调度中的任务列表

-c 可以列出后面接的该项工作的实际命令内容
-f 从文件中读取作业
[root@server ~]# at now +10  minutes
warning: commands will be executed using /bin/sh
at> hello
at> <EOT>
job 2 at Fri Nov  4 09:05:00 2022
[root@server ~]# at -l
2       Fri Nov  4 09:05:00 2022 a root
[root@server ~]# at -v
Garbled time
[root@server ~]# at -c
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...at [-V] [-q x] [-f file] [-mMlbv] -t timeat -c job ...atq [-V] [-q x]at [ -rd ] job ...atrm [-V] job ...batch
[root@server ~]# at -f
at: option requires an argument -- 'f'
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...at [-V] [-q x] [-f file] [-mMlbv] -t timeat -c job ...atq [-V] [-q x]at [ -rd ] job ...atrm [-V] job ...batch
[root@server ~]# at -d 2
[root@server ~]# at -l
[root@server ~]#

时间格式

参数 说明
HH:MM 在今天的HH小时MM分钟执行,如果今天的这个时间点已经过了,则明天执行
HH:MM YYYY-MM-DD 强制规定在某年某月的某一天的特殊时刻进行该工作
now+2minutes 熊现在开始几分钟minutes,hours,days,or weeks
[root@server ~]# at now +1  minutes
warning: commands will be executed using /bin/sh
at> wall hello
at> <EOT>
job 1 at Fri Nov  4 08:46:00 2022Broadcast message from root@server (somewhere) (Fri Nov  4 08:46:00 2022):hello

以上是root用户使用at的方式,接下来演示其他用户使用at的方式

[rehl@server ~]$ touch file `date +%T`.txt
[rehl@server ~]$ ll
total 0
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:31  09:31:02.txt
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Desktop
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Documents
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Downloads
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:31  file
-rw-rw-r--. 1 rehl rehl 0 Oct 30 03:41  file1
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Music
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Pictures
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Public
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Templates
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Videos
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:30 'wn'$'\b''enjian'
[rehl@server ~]$ at now +2 minutes
warning: commands will be executed using /bin/sh
at>  touch file `date +%T`.txt
at>
at> <EOT>
job 5 at Fri Nov  4 09:34:00 2022
[rehl@server ~]$ ll
total 0
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:31  09:31:02.txt
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:34  09:34:00.txt
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Desktop
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Documents
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Downloads
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:34  file
-rw-rw-r--. 1 rehl rehl 0 Oct 30 03:41  file1
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Music
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Pictures
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Public
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Templates
drwxr-xr-x. 2 rehl rehl 6 Oct 16 13:09  Videos
-rw-rw-r--. 1 rehl rehl 0 Nov  4 09:30 'wn'$'\b''enjian'
[rehl@server ~]$

但是其他用户无法达到at的文件所在地,只有root用户可以

[rehl@server ~]$ cd /var/spool/at
-bash: cd: /var/spool/at: Permission denied
[rehl@server ~]$
[root@server ~]# cd /var/spool/at
[root@server at]#

可以在使用root用户在at中查看当前的at进程

[root@server at]# cd ~
[root@server ~]# at now +10 minutes
warning: commands will be executed using /bin/sh
at>  touch file `date +%T`.txt
at> <EOT>
job 6 at Fri Nov  4 09:53:00 2022
[root@server ~]# cd /var/spool/at
[root@server at]# ll
total 4
-rwx------. 1 root root 2792 Nov  4 09:44 a0000601a815c1
drwx------. 2 root root    6 Nov  4 09:34 spool
[root@server at]#

-rwx------. 1 root root 2792 Nov  4 09:44 a0000601a815c1

这个就是当前的at进程,我们可以看到只有root对他有权限,所以其他人无法cd到at。

循环执行的例行性工作

循环执行的例行性工作:每隔一定的周期就需要执行一次。
        循环执行的例行性工作调度是由crond这个系统服务来控制的。同样,我们也可以限制使用crontab的用户账号。

crontab命令的实际工作过程 
1、以账号来判别是否可使用crontab命令
        /etc/cron.allow 将可以使用 crontab 的账号写入其中,若不在该文件内的用户 则不可使用 
crontab
        /etc/cron.deny 将不可以使用 crontab 的账号写入其中,若不在该文件内的用 户则可使用 
crontab。
2、当用户使用 crontab 新建工作调度之后,该项工作就会被记录到/var/spool/cron/ 里面
3、cron 执行的每一项工作都会被 记录到/var/log/cron 这个日志文件中

crontab命令详解 
命令格式: crontab [-u user] [-l | -r | -e]

参数 说明
-u 只有root才能执行这个任务,帮助某个用户新建/删除crontab
-e 编辑crontab的工作内容
-l 查阅crontab的工作内容
-r 删除所有的crontab的工作内容
代表意义 分钟 小时 日期 月份 命令
数字范围 0-59 0-23 1-31 1-12 0-7,0和7都代表周日 执行的命令

文件内的特殊字符

特殊字符 含义
* 代表任何时刻
代表分隔时段
- 代表一段时间范围
/数字 指定时间的间隔频率,例如每3分钟进行一次,*/3

查看crond服务

[root@server ~]# systemctl status crond
● crond.service - Command SchedulerLoaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)Active: active (running) since Fri 2022-11-04 06:55:34 EDT; 3h 32min agoMain PID: 1313 (crond)Tasks: 1 (limit: 4652)Memory: 2.3MCGroup: /system.slice/crond.service└─1313 /usr/sbin/crond -nNov 04 07:01:01 server CROND[2191]: (root) CMD (run-parts /etc/cron.hourly)
Nov 04 07:01:02 server anacron[2200]: Anacron started on 2022-11-04
Nov 04 07:01:02 server anacron[2200]: Will run job `cron.daily' in 9 min.
Nov 04 07:01:02 server anacron[2200]: Jobs will be executed sequentially
Nov 04 07:10:02 server anacron[2200]: Job `cron.daily' started
Nov 04 07:10:02 server anacron[2200]: Job `cron.daily' terminated
Nov 04 07:10:02 server anacron[2200]: Normal exit (1 job run)
Nov 04 08:01:01 server CROND[2927]: (root) CMD (run-parts /etc/cron.hourly)
Nov 04 09:01:01 server CROND[4329]: (root) CMD (run-parts /etc/cron.hourly)
Nov 04 10:01:01 server CROND[7447]: (root) CMD (run-parts /etc/cron.hourly)
[root@server ~]#

crond默认只有黑名单,允许通过的名单需要自己设置

[root@server ~]# ll /etc/cron.allow
ls: cannot access '/etc/cron.allow': No such file or directory
[root@server ~]# ll /etc/cron.deny
-rw-r--r--. 1 root root 0 Jun 12  2019 /etc/cron.deny
[root@server ~]#

查看crond的日志文件

[root@server log]# ll cron
-rw-------. 1 root root 14027 Nov  4 10:01 cron
[root@server log]#

用户的crontab命令设置

[root@server log]# crontab -e
[root@server log]#

使用以上命令来进入编辑器。而在进行编辑之前我们需要前往/etc/crontab查看crontab的编辑格式

[root@server etc]# vim /etc/crontab
[root@server etc]#
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root# For details see man 4 crontabs# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed~
~
~
~
~

按照以上格式来编辑crontab文件

* * * * * wall hello
~
~
~
~

这条命令代表每分钟使用wall发出hello的命令。

[root@server etc]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontabBroadcast message from root@server (somewhere) (Fri Nov  4 10:43:01 2022):hello

其他用户配置crontab文件的方法

首先我们前往/etc/sudoers查看sudo的相关信息

[root@server etc]# vim /etc/sudoers

注意这里,wheel组是默认可以使用sudo命令的,我们可以将其他用户加入这个组,或者将想要使用sudo的用户写入文件中。

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL
[rehl@server ~]$ usermod -G wheel xiaolan

测试sudo是否能够执行。

[xiaolan@server ~]$ sudo ls /var/spool/cron
root
[xiaolan@server ~]$

开始编辑

[xiaolan@server ~]$ sudo crontab -u xiaolan -e
* * * * * touch ~/file`date+\%T`.txt

随后保存退出

[xiaolan@server ~]$ ll
total 0
-rw-r--r--. 1 xiaolan xiaolan 0 Nov  4 11:26 file.txt
[xiaolan@server ~]$

系统的例行性任务

系统的例行性任务我们需要前往/etc/crontab文件编辑

[root@server ~]# vim /etc/crontab
[root@server ~]#
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root# For details see man 4 crontabs# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
* * * * * root wall hello
[root@server ~]# date
Fri Nov  4 11:31:55 EDT 2022Broadcast message from root@server (somewhere) (Fri Nov  4 11:32:01 2022):hello

当需要执行多个任务时,先创建一个文件,将任务放到文件中,随后在调用该文件实现多个任务同时执行。

[root@server ~]# vim 1.sh
touch ~/file`date +%T`.txt
mkdir  ~/dir`date +\%T`
~
~
~
~
~
~
[root@server ~]# ll
total 20
-rw-r--r--. 1 root root  197 Oct 30 03:25 '!'
-rw-r--r--. 1 root root   51 Nov  4 11:41  1.sh
-rw-------. 1 root root 1382 Oct 16 12:54  anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Desktop
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Documents
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Downloads
-rw-r--r--. 1 root root    0 Oct 30 03:40  file1
-rw-r--r--. 1 root root    0 Nov  1 23:41  file2
-rw-r--r--. 1 root root 1654 Oct 16 13:04  initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Music
-rw-r--r--. 1 root root 3283 Oct 28 11:28  myfile.zip
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Pictures
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Public
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Templates
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Videos

在这里我们可以看见1.sh的文件权限为-rw-r--r--,为了crontab能够顺利执行,我们需要给1.sh改变权限。

[root@server ~]# chmod a+x 1.sh
[root@server ~]# ll
total 20
-rw-r--r--. 1 root root  197 Oct 30 03:25 '!'
-rwxr-xr-x. 1 root root   51 Nov  4 11:41  1.sh
-rw-------. 1 root root 1382 Oct 16 12:54  anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Desktop
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Documents
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Downloads
-rw-r--r--. 1 root root    0 Oct 30 03:40  file1
-rw-r--r--. 1 root root    0 Nov  1 23:41  file2
-rw-r--r--. 1 root root 1654 Oct 16 13:04  initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Music
-rw-r--r--. 1 root root 3283 Oct 28 11:28  myfile.zip
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Pictures
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Public
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Templates
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Videos
[root@server ~]#
[root@server ~]# ll
total 20
-rw-r--r--. 1 root root  197 Oct 30 03:25 '!'
-rwxr-xr-x. 1 root root   51 Nov  4 11:41  1.sh
-rw-------. 1 root root 1382 Oct 16 12:54  anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Desktop
drwxr-xr-x. 2 root root    6 Nov  4 11:47  dir11:47:01
drwxr-xr-x. 2 root root    6 Nov  4 11:48  dir11:48:01
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Documents
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Downloads
-rw-r--r--. 1 root root    0 Oct 30 03:40  file1
-rw-r--r--. 1 root root    0 Nov  4 11:47  file11:47:01.txt
-rw-r--r--. 1 root root    0 Nov  4 11:48  file11:48:01.txt
-rw-r--r--. 1 root root    0 Nov  1 23:41  file2
-rw-r--r--. 1 root root 1654 Oct 16 13:04  initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Music
-rw-r--r--. 1 root root 3283 Oct 28 11:28  myfile.zip
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Pictures
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Public
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Templates
drwxr-xr-x. 2 root root    6 Oct 16 13:28  Videos
[root@server ~]#

chrony服务器

Chrony是一个开源自由的网络时间协议 NTP 的客户端和服务器软软件。它能让计算机保持系统时钟与时钟服务器(NTP)同步,因此让你的计算机保持精确的时间,Chrony也可以作为服务端软件为其他计算机提供时间同步服务。
        Chrony由两个程序组成,分别是chronyd和chronyc
        chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
        chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。
        NTP 是网络时间协议(Network Time Protocol)的简称,通过 udp 123 端口进行网络时钟同步。
        RHEL7中默认使用chrony作为时间服务器,也支持NTP,需要额外安装。
        NTP与chrony不能同时存在,只能用其中一个。

安装与配置 

首先,我们需要查看本机是否有chrony这个文件包,如果没有需要自行下载安装。

[root@server ~]# rpm -qa | grep chrony
chrony-4.1-1.el8.x86_64
[root@server ~]#

查看chrony状态

[root@server ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enab>Active: active (running) since Fri 2022-11-04 06:55:26 EDT; 5h 9min agoDocs: man:chronyd(8)

如果处于关闭状态需要启动它,可以输入systemctl start chronyd来重启chrony。

同步时间,带*表示启用的时间服务器。


[root@server ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^+ ntp0.icolo.io                 2  10   275   135  -9570us[-9791us] +/-  216ms
^* mail.rettensteiner.com        2  10   275   106    +28ms[  +28ms] +/-  176ms
^+ eudyptula.init7.net           2  10   337  1105    +21ms[  +21ms] +/-  182ms
^+ mail.light-speed.de           2  10   337   625    -12ms[  -12ms] +/-  160ms
[root@server ~]# date
Fri Nov  4 12:07:37 EDT 2022
[root@server ~]#

如果需要同步其他的时间服务器,我们需要进入/etc/chrony.conf文件进行配置

[root@server ~]# vim /etc/chrony.conf

找到这里

# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool 2.rhel.pool.ntp.org iburst

更改为已知的时间服务器,这里以阿里云为例

# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool ntp.aliyun.com iburst

为保证程序能够达到预期效果,我们需要检查网络是否畅通

[root@server ~]# ping ntp.aliyun.com
PING ntp.aliyun.com (203.107.6.88) 56(84) bytes of data.
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=1 ttl=128 time=84.1 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=2 ttl=128 time=75.5 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=3 ttl=128 time=76.1 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=4 ttl=128 time=71.2 ms
64 bytes from 203.107.6.88 (203.107.6.88): icmp_seq=5 ttl=128 time=68.4 ms--- ntp.aliyun.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4008ms
rtt min/avg/max/mdev = 68.357/75.055/84.141/5.369 ms
[root@server ~]#

为使程序正常运行,我们需要重启chronyd

[root@server ~]# systemctl restart chronyd
[root@server ~]#

我们可以通过systemctl ststus chronyd这个命令来查看chronyd的运行情况

[root@server ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enab>Active: active (running) since Fri 2022-11-04 12:15:44 EDT; 1min 43s agoDocs: man:chronyd(8)man:chrony.conf(5)Process: 10154 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exite>Process: 10163 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, st>Process: 10159 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 10161 (chronyd)Tasks: 1 (limit: 4652)Memory: 1.0MCGroup: /system.slice/chronyd.service└─10161 /usr/sbin/chronydNov 04 12:15:44 server systemd[1]: Starting NTP client/server...
Nov 04 12:15:44 server chronyd[10161]: chronyd version 4.1 starting (+CMDMON +NTP +REFCL>
Nov 04 12:15:44 server chronyd[10161]: Frequency -1.446 +/- 0.408 ppm read from /var/lib>
Nov 04 12:15:44 server chronyd[10161]: Using right/UTC timezone to obtain leap second da>
Nov 04 12:15:44 server systemd[1]: Started NTP client/server.
Nov 04 12:15:49 server chronyd[10161]: Selected source 203.107.6.88 (ntp.aliyun.com)
Nov 04 12:15:49 server chronyd[10161]: System clock TAI offset set to 37 seconds
lines 1-21/21 (END)

我们可以看见它选择了ntp.aliyun.com作为自己的源。


[root@server ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88                  2   6   377     7  +3960us[+7407us] +/-   47ms
[root@server ~]#

为了保证服务器的稳定性,我们一般会加载多个时间服务器

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool ntp.aliyun.com iburst
pool ntp.tencent.com iburst
pool 210.72.145.44 iburst

在写完之后我们都需要重启下这个进程。

时间服务器的相关配置

[root@server ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^+ 203.107.6.88                  2   7   375    32  +1911us[+1911us] +/-   50ms
^* 139.199.215.251               2   9   327   741  +1084us[ +235us] +/-   46ms
^? 210.72.145.44                 0  10     0     -     +0ns[   +0ns] +/-    0ns
[root@server ~]#

从上方的代码我们可以看见一个名为Stratum的参数,它代表的是列表中的时间服务器属于第几层,层数一共是0-15层。在之前的操作中我们已经将客户端的时间服务器配置完毕,接下来我们配置服务端的时间服务器。

在设置时间服务器之前,我们需要先确保两台虚拟机之间的网络是畅通的

[root@server ~]# ping 192.168.240.130
PING 192.168.240.130 (192.168.240.130) 56(84) bytes of data.
64 bytes from 192.168.240.130: icmp_seq=1 ttl=64 time=0.497 ms
64 bytes from 192.168.240.130: icmp_seq=2 ttl=64 time=0.361 ms
64 bytes from 192.168.240.130: icmp_seq=3 ttl=64 time=0.305 ms
64 bytes from 192.168.240.130: icmp_seq=4 ttl=64 time=0.371 ms--- 192.168.240.130 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3057ms
rtt min/avg/max/mdev = 0.305/0.383/0.497/0.072 ms
[root@server ~]#
[root@localhost ~]# ping 192.168.240.128
PING 192.168.240.128 (192.168.240.128) 56(84) bytes of data.
64 bytes from 192.168.240.128: icmp_seq=1 ttl=64 time=0.397 ms
64 bytes from 192.168.240.128: icmp_seq=2 ttl=64 time=1.03 ms
64 bytes from 192.168.240.128: icmp_seq=3 ttl=64 time=0.398 ms
64 bytes from 192.168.240.128: icmp_seq=4 ttl=64 time=0.365 ms--- 192.168.240.128 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3109ms
rtt min/avg/max/mdev = 0.365/0.548/1.032/0.279 ms
[root@localhost ~]#

接下来我们以128这个网段的虚拟机作为服务端,130网段的虚拟机所谓客户端。服务端需要关闭防火墙

[root@server ~]# systemctl stop firewalld
[root@server ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: en>Active: inactive (dead) since Sat 2022-11-05 08:36:43 EDT; 16s agoDocs: man:firewalld(1)Process: 979 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exit>Main PID: 979 (code=exited, status=0/SUCCESS)Nov 05 08:15:35 server systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 05 08:15:38 server systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 05 08:15:39 server firewalld[979]: WARNING: AllowZoneDrifting is enabled. This is co>
Nov 05 08:36:42 server systemd[1]: Stopping firewalld - dynamic firewall daemon...
Nov 05 08:36:43 server systemd[1]: firewalld.service: Succeeded.
Nov 05 08:36:43 server systemd[1]: Stopped firewalld - dynamic firewall daemon.
lines 1-13/13 (END)

随后在服务端书写时间服务器的配置文件

[root@server ~]# vim /etc/chrony.conf

找到这里,这里是决定ntp是否能够通过的代码段。

# Allow NTP client access from local network.
allow 192.168.168.0/24

重启服务

[root@server ~]# systemctl restart chronyd
[root@server ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enab>Active: active (running) since Sat 2022-11-05 08:43:20 EDT; 33s agoDocs: man:chronyd(8)man:chrony.conf(5)Process: 3345 ExecStopPost=/usr/libexec/chrony-helper remove-daemon-state (code=exited>Process: 3353 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, sta>Process: 3350 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 3352 (chronyd)Tasks: 1 (limit: 4652)Memory: 1.5MCGroup: /system.slice/chronyd.service└─3352 /usr/sbin/chronydNov 05 08:43:16 server systemd[1]: Starting NTP client/server...
Nov 05 08:43:16 server chronyd[3352]: chronyd version 4.1 starting (+CMDMON +NTP +REFCLO>
Nov 05 08:43:18 server chronyd[3352]: Frequency -2.725 +/- 6.674 ppm read from /var/lib/>
Nov 05 08:43:20 server chronyd[3352]: Using right/UTC timezone to obtain leap second data
Nov 05 08:43:20 server systemd[1]: Started NTP client/server.
Nov 05 08:43:27 server chronyd[3352]: Selected source 203.107.6.88 (ntp.aliyun.com)
Nov 05 08:43:27 server chronyd[3352]: System clock TAI offset set to 37 seconds
Nov 05 08:43:30 server chronyd[3352]: Selected source 139.199.215.251 (ntp.tencent.com)
lines 1-22/22 (END)

接下来配置客户端的设置,首先查看chrony包是否已经装载

[root@localhost ~]# rpm -qa |grep chrony
chrony-4.1-1.el8.x86_64
[root@localhost ~]#
[root@localhost ~]# vim /etc/chrony.conf

配置服务器地址

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#pool 2.rhel.pool.ntp.org iburst
pool 192.168.240.128 iburst
[root@localhost ~]# systemctl restart chronyd

验证

[root@localhost ~]# date -s '5:00'
Sat Nov  5 05:00:00 EDT 2022[root@localhost ~]# systemctl status chronyd
● chronyd.service - NTP client/serverLoaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)Active: active (running) since Sat 2022-11-05 08:52:56 EDT; 3h 51min leftDocs: man:chronyd(8)man:chrony.conf(5)Process: 1040 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SU>Process: 1006 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)Main PID: 1033 (chronyd)Tasks: 1 (limit: 4636)Memory: 740.0KCGroup: /system.slice/chronyd.service└─1033 /usr/sbin/chronydNov 05 08:52:56 localhost.localdomain systemd[1]: Starting NTP client/server...
Nov 05 08:52:56 localhost.localdomain chronyd[1033]: chronyd version 4.1 starting (+CMDMON +NTP >
Nov 05 08:52:56 localhost.localdomain chronyd[1033]: Frequency 2.068 +/- 32.437 ppm read from /v>
Nov 05 08:52:56 localhost.localdomain chronyd[1033]: Using right/UTC timezone to obtain leap sec>
Nov 05 08:52:56 localhost.localdomain systemd[1]: Started NTP client/server.
Nov 05 08:53:05 localhost.localdomain chronyd[1033]: Source 192.168.240.128 offline
Nov 05 08:57:03 localhost.localdomain chronyd[1033]: Source 192.168.240.128 online
Nov 05 05:00:07 localhost.localdomain chronyd[1033]: Backward time jump detected!

从这里我们可以看见客户端已经选择了128这个网段为源

[root@localhost ~]# date
Sat Nov  5 05:17:06 EDT 2022
[root@localhost ~]# chronyc -a makestep
200 OK
[root@localhost ~]# date
Sat Nov  5 09:18:01 EDT 2022
[root@localhost ~]# systemctl enable chronyd --now设置开机启动并立刻启动
[root@localhost ~]#

练习:

在每天早上9点,让你的服务器可以通过ntp.aliyun.com和ntp.tencent.com同步自己的时间。

首先要配置好时间服务器

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
pool ntp.aliyun.com iburst
pool ntp.tencent.com iburst
pool 210.72.145.44 iburst

测试时间是否同步

[root@server ~]#  chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^+ 203.107.6.88                  2   9   377    42  -4520us[-4814us] +/-   47ms
^* 139.199.215.251               2   8   177    35   +672us[ +378us] +/-   33ms
^? 210.72.145.44                 0   9     0     -     +0ns[   +0ns] +/-    0ns
[root@server ~]#

设置开机自启

[root@server ~]# systemctl enable chronyd --now
[root@server ~]#

书写计划命令

[root@server ~]# vim /etc/crontab
[root@server ~]#
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root# For details see man 4 crontabs# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
* 9 * * * root  chronyc sources
~
~

这样,在每天的九点钟就可以自行进行时间校准了。

以上就是本次的全部内容,谢谢观看

Linux高级课程----Linux的例行性工作和chrony服务器相关推荐

  1. 鸟哥的Linux私房菜(基础篇)- 第十六章、例行性工作排程 (crontab)

    第十六章.例行性工作排程 (crontab) 最近升级日期:2009/09/11 学习了基础篇也一阵子了,你会发现到为什么系统常常会主动的进行一些任务?这些任务到底是谁在配置工作的?如果你想要让自己设 ...

  2. Linux学习笔记39——任务调度:什么是例行性工作调度、仅执行一次的工作调度、循环执行的例行性工作调度、可唤醒停机期间的工作任务

    一.什么是例行性工作调度 1,Linux 工作调度的种类: at, cron 2,CentOS Linux 系统上常见的例行性工作 二,仅执行一次的工作调度 1,atd 的启动与 at 运行的方式 a ...

  3. 例行性工作排程 (crontab)

    http://linux.vbird.org/linux_basic/0430cron.php 1. 什麼是例行性工作排程 1.1 Linux 工作排程的種類: at, crontab 1.2 Lin ...

  4. linux 例行性工作,Linux例行性工作

    Linux例行性工作 Linux:例行性工作 什么是例行性工作? 其实对于例行性工作而言就是在当前环境下周期性进行的必须工作. 在Linux下,有2种例行性工作种类: 一种是突发性,做完以后就没有的那 ...

  5. Linux的例行性工作

    在Linux后台中有许多的默认执行的系统服务,就像我们Windows开机时就有一个exeplorer.exe默认运行,这些系统服务称为系统守护进程. 接下来介绍两个比较重要的系统守护进程atd和cro ...

  6. Linux上使用at单一执行例行性工作,使用crontab循环执行例行性任务以及系统例行任务

    在工作或学习中,我们经常需要对重要的文件进行备份,或者做一些日常性的排查等工作,了解如何在linux上执行例行性工作显得尤为重要! 本文基于Linux上CentOS 7版本进行演示 一.单一执行例行性 ...

  7. RHCE第一天之Linux例行性工作at、crontab详解

    文章目录 一.学习内容总结 1.单一执行的例行性工作at 2.循环执行的例行性工作crontab 二.作业 at和crontab的使用 一.学习内容总结 1.单一执行的例行性工作at 概念: 指仅处理 ...

  8. 【树莓派不吃灰】Linux篇⑩ 学习例行性工作排程(核心概念)

    目录 1. 什么是例行性工作排程 2. 仅运行一次的工作排程 3. 循环运行的例行性工作排程 4. 可唤醒停机期间的工作任务 5. 重点回顾 ❤️ 博客主页 单片机菜鸟哥,一个野生非专业硬件IOT爱好 ...

  9. 例行性工作 排程(crontab)

    一,什么是例行性工作排程? 计划任务:比如每天,每周,每月,每年或者指定时间需要执行的工作任务 1,Linux工作排程的种类:at,cron a)at :at 处理仅执行一次任务,依赖于atd服务 b ...

最新文章

  1. mate30升级鸿蒙系数据会被清空吗,145直接升级鸿蒙会不会掉资料
  2. 【干货】最高级的运营,就是用科学的方法做艺术
  3. ITK:使用Deformation Field扭曲图像
  4. 你是如何坚持读完《算法导论》这本书的?(帖子收集)
  5. linux phpunit 安装,PHPUnit安装教程
  6. Jozky 刷题目录
  7. Java EE 7社区调查结果!
  8. 文件名(后缀名)修改
  9. 优化:代码移动code motion
  10. 个人量化策略整理_较好
  11. 应对计算机领域中后门,网络课程论坛中长尾现象的应对策略——以《计算机应用基础》网络课程为例...
  12. IBM分布式深度学习技术将语音识别训练时间从一周缩短到11小时
  13. 搜索数据android ui,搜索概览  |  Android 开发者  |  Android Developers
  14. i9-10900K比9900K性能提升了多少?i9-10900K和i9-9900K区别对比评测
  15. CC2500的CCA
  16. java线程生命周期的图示以及文字说明
  17. 局域网文件服务器单独文件夹加密,局域网 如何给共享文件夹加密
  18. macbook 终端命令怎么使用_苹果MAC系统怎么使用ping命令打开终端?
  19. nvaicat 下载加破解教程
  20. 大数据处理过程之核心技术ETL详解

热门文章

  1. System Verilog约束块(constrain block)控制和随机变量的随机属性控制
  2. 使用nginx代理访问外网
  3. 三、IDEA更换主题皮肤
  4. AVplayer断网播放出错时player的duration、playableDuration、totalTime
  5. 防抖节流的实现与区分(详解带代码)
  6. 什么是网络上的Cookies,您如何使用它们?
  7. 呃呃呃呃呃呃鹅鹅鹅鹅鹅鹅饿
  8. 藏书于海(共享图书)--适度
  9. 简述人工神经网络的作用,简述人工神经网络
  10. c语言中的二目运算符,C语言中的三目运算符是什么