Linux操作系统

本文详细介绍了Linux中Crontab计划任务的使用,包括时间日期设置、服务状态查看、用户定时任务的格式与管理。同时,讲解了NTP时间同步服务的设置,如修改系统时区、时间。此外,还涉及了一次性计划任务at的创建、查看、删除等操作。文章最后讨论了timedatectl命令的用法,以及NTP服务配置文件的查找方法。
摘要由CSDN通过智能技术生成

Linux操作系统实验报告7(Crontab计划任务+NTP时间同步服务器)

一、用户定时任务的使用
1.timedatectl命令的用法

timedatectl  //查看、设置系统时间日期

例:如何修改Linux系统时间
发现直接修改受限,需按以下步骤进行操作
2.设置时间

timedatectl  set-time 11:00 //Failed NTP     Network time protocal网络时间协议
timedatectl  set-ntp false                       //关闭NTP时间同步
timedatectl  set-time 11:00 //OK
timedatectl 

在这里插入图片描述
3.查看服务基本信息

systemctl status crond.service   //查看周期性计划任务的服务状态,running

在这里插入图片描述

systemctl enable --now crond  //设置周期性计划任务crond为开机自启动,并且当前立即开启
rpm -qa 查看已安装的全部软件包

在这里插入图片描述

rpm -qa | grep cron   //使用管道在所有的已安装包中搜索包含cron的软件包,找到crontabs-1.11-16.20150630git.el8.noarch

rpm -qc crontabs-1.11-16.20150630git.el8.noarch    //查看crontabs-1.11-16.20150630git.el8.noarch包的相关配置文件,可以找到配置文件
(列出相关配置文件)/etc/crontab

在这里插入图片描述

4.用户定时任务的格式
Minute Hour Day Month DayofWeek Command
vim /etc/crontab
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)   24小时制  下午5点=17点
# |  |  .---------- 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   *表示任何时间

在这里插入图片描述
注:command操作命令格式 绝对路径+命令
拓展:用which搜索绝对路径
关机shutdown -h now
5.设置用户定时任务的步骤:
切换用户身份
crontab命令(-e;-l;-r)
(1)crontab -l List列表查看用户定时任务,未设置前显示no
在这里插入图片描述
(2)crontab -e edit编辑录入使用vim
在这里插入图片描述
(3)Crontab -r remove 全部删除
在这里插入图片描述
注:切换用户的两种方式 su - username;直接以用户身份登录
在这里插入图片描述
拓展:date输出重定向
在这里插入图片描述
Command指令输入重定向命令,可以把时间写入指定文件中
在这里插入图片描述
使用cat查看内容
在这里插入图片描述
6.实例
a.zhangsan用户创建定时任务,要求:每天下午5:00关机。
0 17 * * 0-6 shutdown -h now
b.lisi:周一至周五朝九晚五每分钟执行命令date >> /tmp/bgl1.txt。

  • 9-16 * * 1-5 date >> /tmp/gj1.txt
    c.lisi用户创建定时任务,要求:周一至周五朝九晚五每两小时执行命令date >> /tmp/gj1.txt。
    0 9-17/2 * * 1-5 date >> /tmp/gj1.txt
    在这里插入图片描述
    d.lisi用户创建定时任务,要求:周一至周五朝九晚五每隔10分钟发一条消息。
    */10 9-17 * * 1-5 date >> /tmp/gj2.txt
    e.lisi用户创建定时任务,要求:周一至周五1点,3点,9点每隔10分钟发一条消息。
    */10 1,3,9 * * 1-5 date >> /tmp/gj1.txt
    在这里插入图片描述
    f.lisi用户创建定时任务,要求:每年2月2日上午9点执行echo hello。
    0 9 2 2 * echo hello
    g.lisi用户创建定时任务,要求:每天3到6点2分 执行一个脚本/root/1.sh
    2 3-6 * * * /bin/sh /root/1.sh
    h.lisi用户创建定时任务,要求:每两个小时的第2分钟,执行一个脚本/root/1.sh
    2 */2 * * * /bin/sh /root/1.sh
    课后作业:配置cron任务,每隔2分钟运行logger “Ex200 in progress”,以harry用户身份运行

这里注意直接以Harry 身份crontab -l发现提示错误信息,需要先把Harry添加到白名单才能继续操作。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
7.管理员对用户定时任务的管理方法
方法一:/var/spool/cron 用户定时任务的工作目录(查看;修改;删除)
在这里插入图片描述
删除rm -f ,再回zhangsan用户查看定时任务发现已删除
在这里插入图片描述
在这里插入图片描述
方法二:crontab -u lisi -l|-e|-r
在这里插入图片描述
7.用户定时任务的高级管理技巧
黑名单与白名单的用法(二者择其一使用)
黑名单文件/etc/cron.deny
在这里插入图片描述
在这里插入图片描述
白名单文件/etc/cron.allow
在这里插入图片描述

systemctl restart crond 

重启生效(8版本以前)
在这里插入图片描述
二、系统定时任务的用法
(1)系统定时任务的工作目录
/etc/cron.daily/ 该目录下的脚本文件每天会自动运行一次(触发时间:4:22);
logrotate日志回滚、压缩日志、通过电子邮件发送日志、删除日志、归档日志,并在你需要时开始记录最新的。
Tmpwatch 删除暂存文件
/etc/cron.hourly/ 该目录下的脚本文件每小时(第一分钟)会自动运行一次
/etc/cron.weekly 该目录下的脚本文件每星期(周日的4:22)会自动运行一次
/etc/cron.monthly/ 该目录下的脚本文件每个月(1号的4:22)会自动运行一次
(2)anacron系统
原理:系统定时任务的触发时间由于某种原因(例如停电)而错过了,anacron系统会保证
系统定时任务工作目录下的脚本会弥补运行的一种机制。
vim /etc/anacrontab

三、一次性(临时)计划任务at(在servera上做实验)
(1)查看服务基本信息

systemctl status atd    //一次性计划任务at的服务名称叫atd,running

在这里插入图片描述
在这里插入图片描述

rpm -q at  //查询at是否安装,软件包信息
rpm -qc at  //查询at服务的配置文件

(2)建立一次性计划任务(只执行一次)

at 09:20
warning: commands will be executed using /bin/sh
at> mkdir /tmp/zhangsan
at> touch /tmp/lisi.txt
at> <EOT>
job 1 at Fri Nov 20 09:20:00 2020

ctrl+d结束一次性计划任务的录入
小技巧:脚本交互状态时的退格删除按键是ctrl+Backspace
(3)查看一次性计划任务的列表

at -l(或者atq)如果一次性任务执行完成就查看不到

在这里插入图片描述
1 Fri Nov 20 09:20:00 2020 a root
2 Fri Nov 20 08:50:00 2020 a root
(4)查看一次性计划任务的内容

at -c 1 (序号)  //任务内容在倒数第三行

(5)删除一次性计划任务

at -d 1(等效于atrm -1)

(6)其他用法

[root@servera ~]# at 17:20 tomorrow  明天这个时候
warning: commands will be executed using /bin/sh
at> echo abc
at> <EOT>
job 5 at Sat Nov 21 17:20:00 2020


注意:echo abc命令在指定时间会执行,但是无法看 到效果,因为不是在当前shell执行的


at 5:10pm+3 days   //设置一次性任务的执行时间是3天后的下午5:10
echo "hello" >> /tmp/zhangsan1.txt | at now +1min   //设置一次性任务的执行时间是1分钟后,追加输出字符串hello到目标文件中。
watch at -l  //监控各个没有运行的一次性任务,ctrl+c退出监控状态

(7)黑名单文件/etc/at.deny

四、timedatectl命令的用法
(1)查看时区

timedatectl list-timezones   //列出时区信息,q退出

在这里插入图片描述

timedatectl list-timezones | grep -i  asia     //-i,表示忽略大小写

在这里插入图片描述
(2)设置时区

timedatectl  set-timezone Asia/Tokyo   //设置系统时区,连网才会显示出真实的时区时间
timedatectl 

在这里插入图片描述
(3)设置时间

timedatectl  set-time 11:00 //Failed NTP
timedatectl  set-ntp false                       //关闭NTP时间同步
timedatectl  set-time 11:00 //OK
timedatectl 

timedatectl  set-ntp true                       //开启NTP时间同步
timedatectl  set-timezone Asia/Shanghai   //设置系统时区为亚洲/上海,1分钟内系统时区会发生变化
timedatectl
systemctl status chronyd.service   //查看NTP时间同步服务是否运行

在这里插入图片描述
2.如何倒查某个服务的配置文件(例如NTP服务)(在servera上做实验)

timedatectl set-ntp true   //首先需要启动该ntp服务
systemctl list-units | grep -i ntp   //查询ntp服务来源于哪个系统模块,结果显示是chronyd.service模块
systemctl status chronyd.service   //查看chronyd.service的状态为running,并且上面显示了其服务文件为/usr/lib/systemd/chronyd.service
systemctl start chronyd.service
systemctl enable --now chronyd.service
rpm -qf  /usr/lib/systemd/chronyd.service   //查看该服务源于哪个包,这里是chrony-3.3-3.el8.x86_64
rpm -qc  chrony-3.3-3.el8.x86_64   //查看包chrony-3.3-3.el8.x86_64的相关配置文件,可以找到配置文件/etc/chrony.conf
vim  /etc/chrony.conf

在这里插入图片描述
3.如何设置NTP服务客户端(在servera上做实验)

timedatectl  set-ntp true                       //开启NTP时间同步
timedatectl     //查看NTP服务的状态为active
systemctl status chronyd.service   //查看NTP时间同步服务是否运行
vim  /etc/chrony.conf      //编辑NTP服务指向文件
server 172.25.254.250 iburst   //设置时钟同步服务器是172.25.254.250,也可以设置成172.25.254.254或者classroom.example.com

在这里插入图片描述

systemctl restart chronyd.service   //重启chronyd服务,即NTP服务
timedatectl     //查看system clock synchronized的值是否为yes
chronyc sources -v   //验证当前使用的是哪个时钟同步服务器

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值