2.4 系统定时任务及延时任务

1.时间管理命令

     at /etc/adjtime
     timedatectl
     timedatectl set-local-rtc 0                       不使用rtc时间
     timedatectl set-timezone "Asia/Tokyo"  设置时区
     timedatectl
     timedatectl set-time "2021-11-11 11:11:11"                     
     systemctl status chronyd.service             查看时间同步服务
     systemctl stop chronyd.service                  关闭服务
     timedatectl set-time "2021-11-11 11:11:11"
     timedatectl
     systemctl restart chronyd.service    重新开启时间同步服务         
     timedatectl
     systemctl status chronyd.service     查看服务状态
     timedatect

2、时间同步

服务器:
      systemctl stop firewalld          关闭防火墙
     vim /etc/chrony.conf              查看配置文件
     systemctl restart chronyd.service   重启服务
--------------------------------------------------------
配置文件:
allow 0.0.0.0/0                允许同步至所有主机
# Serve time even if not synchronized to a time source.
local stratum 10                  去掉##

接收端:
       vim /etc/chrony.conf
       systemctl restart chronyd.service
------------------------------------------------
配置文件:pool 172.25.254.232 iburst    

(实验结果正确)

3、系统延时

       at (now+1min .......)
       vim /etc/at.deny   黑名单
       vim /etc/at.allow  白名单

     

4、系统定时

#/var/spool/cron/username  ##任务保存文件
crontab -u username -e
crontab -u username -l
crontab -u username -r

1)crontab时间表(分钟小时天月周)

* * * * *         ##每分钟
*/2 * * * *        ##每两分钟
*/2 09-17 * * *     ##早九到晚五每两分钟
*/2 09-17 3,5 2 5   ##二月的三号和五号以及每周五早九到晚5每隔两分钟

 

2) 黑白名单

3)定时时不发送邮件(两种方法)、不生成日志

dnf install postfix mailx     安装软件

systemctl enable --now postfix    立即启动软件

不发送邮件的第一种方法:设置定时任务时直接将命令丢入/dev/null 或MAILTO='  '

 

第二种方法:关闭定时任务的发送邮件功能 /etc/sysconfig/crond

 4)系统级别的cron

 

 5)临时文件管理

cd /lib/tmpfiles.d/
vim westos.conf   编辑临时文件规则
d /mnt/westos 1777 root root 6s
systemd-tmpfiles --create /lib/tmpfiles.d/westos.conf  执行规则,新建/mnt/westos目录
watch -n 1 ls -lR /mnt
在/mnt/westos下新建file1 file2文件
然后建立file3文件,并且立刻执行systemd-tmpfiles --clean /lib/tmpfiles.d/westos.conf清理不符合规则的文件
file1 file2被删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux中,我们可以使用cron和at命令来创建定时任务延时任务。cron是一个在后台运行的守护进程,它可以根据预定的时间表执行命令或脚本。而at则是一种命令行工具,可以在指定的时间执行一次性任务。 1. 定时任务 在Linux系统中,定时任务可以使用cron命令进行配置。cron命令允许用户在指定的时间间隔内从命令行或脚本中运行命令。 在cron中,时间间隔由5个字段来定义: ``` * * * * * command to be executed - - - - - | | | | | | | | | +----- day of the week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of the month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59) ``` 例如,要在每周一的早上5点运行一个脚本,可以使用以下命令: ``` 0 5 * * 1 /path/to/script.sh ``` 这将在每天的5:00 AM执行/path/to/script.sh脚本,只有当日期为周一时才会执行。 2. 延时任务 在Linux系统中,我们可以使用at命令来创建延时任务。at命令允许用户在指定的时间运行一次性任务。 使用at命令创建一个延时任务的基本语法格式如下: ``` at TIME <<EOF command1 command2 ... EOF ``` 其中TIME可以是绝对时间或相对时间,也可以使用日期和时间的组合。例如,以下命令将在10分钟后运行命令: ``` at now + 10 minutes <<EOF /path/to/command EOF ``` 此外,也可以使用at命令来指定一个具体的时间运行命令: ``` at 2:00am tomorrow <<EOF /path/to/command EOF ``` 这将在明天的凌晨2点运行命令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值