0
点赞
收藏
分享

微信扫一扫

定时任务管理


定时任务管理

1. 定时任务的概念
设定指定的时间周期性执行你的计划或者任务

两种:

系统定时任务: 定时清理文件 收集系统信息 定时切割日志

用户定时任务: 同步时间 定时备份数据

3. 定时任务相关介绍
[root@web01 conf.d]# ll -d /etc/cron
cron.d/ cron.deny cron.monthly/ cron.weekly/
cron.daily/ cron.hourly/ crontab
注:存放的系统定时任务的分时日月周

[root@mm ~]# cat /etc/crontab
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@mm ~]# ll /var/spool/cron/root #存放定时任务的配置文件
total 0

[root@mm ~]# ll /var/log/cron #定时任务执行的过程 日志


[root@mm ~]# ll /var/spool/mail/ #用户的邮件


crontab #书写定时任务的命令

选项:

-e #编辑定时任务 === vi /var/spool/cron/root

-l #查看定时任务 === cat /var/spool/cron/root

#定时任务的规则

* # 每(分时日月周)都执行

*/5 # 每 5 (分时日月周)执行 每隔多长时间

/5

1-5 #时间范围 1-5 连续的时间 1点到3点

4 、定时同步系统时间 每分钟同步

定时任务最好加上注释 作者 时间

[root@2021 ~]# crontab -l
#每天每隔三个小时时间同步一次
00 */3 * * * /usr/sbin/ntpdate ntp.aliyun.com


#查看定时任务的执行过程
[root@2021 ~]# tail -f /var/log/cron
Apr 1 09:33:02 2021 run-parts(/etc/cron.daily)[1444]: starting mlocate
Apr 1 09:33:03 2021 run-parts(/etc/cron.daily)[1474]: finished mlocate
Apr 1 09:33:03 2021 anacron[1434]: Job `cron.daily' terminated
Apr 1 09:33:03 2021 anacron[1434]: Normal exit (1 job run)
Apr 1 10:01:01 2021 CROND[1485]: (root) CMD (run-parts /etc/cron.hourly)
Apr 1 10:01:01 2021 run-parts(/etc/cron.hourly)[1485]: starting 0anacron
Apr 1 10:01:01 2021 run-parts(/etc/cron.hourly)[1494]: finished 0anacron
Apr 1 17:53:36 2021 crond[718]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 23% if used.)


#邮件正在一直接收信息

[root@mm~]# ll /var/spool/mail/root
-rw------- 1 root mail 7028 Jul 31 10:35 /var/spool/mail/root

总结:

1. 定时任务要有注释 作者 时间

2. 定时任务的命令一定要在命令行上面执行成功

3. 定时任务要使用绝对路径

4. 定时任务写命令的时候,尽量复制之前执行成功的命令 减少出错率

5. 定时任务的执行结果定向到指定的文件中或者定向到空

6.定时任务中,有些特殊字符不识别,需要转义

4. 定时发邮件

[root@mm ~]# yum install -y mailx

[root@mm~]# vim /etc/mail.rc
1.服务端配置邮件功能vim /etc/mail.rc
1 set from=171554782@qq.com #更改为你的邮箱
2 set smtp=smtps://smtp.qq.com:465
3 set smtp-auth-user=171554782@qq.com #更改为你的邮箱
4 set smtp-auth-password=nvaeranunveledja #更改为你的邮箱密文(查看复制)
5 set ssl-verify=ignore
6 set nss-config-dir=/etc/pki/nssdb/

[root@mm ~]# mail -s "what are you doing?" 1710724925@qq.com < 1.txt


举报

相关推荐

0 条评论