@linux定时任务介绍及使用

1. 源码包管理

1. 要有源码包  下载源码包

[root@qls ~]# wget  http://nginx.org/download/nginx-1.18.0.tar.gz



[root@qls ~]# ll
total 1016
-rw-r--r-- 1 root root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz

2. 解压软件包 

[root@qls ~]# tar  xf  nginx-1.18.0.tar.gz 
[root@qls ~]# ll
total 1016
drwxr-xr-x 8 user05 1001     158 Apr 21 22:09 nginx-1.18.0
-rw-r--r-- 1 root   root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz


3. 进入这个目录,进行预编译操作   编译设置  

[root@qls nginx-1.18.0]# ./configure   --prefix=/opt/nginx-1.18.0   --with-http_ssl_module
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found		#报错   缺少编译软件 gcc



#按照依赖

[root@qls nginx-1.18.0]# yum install  -y  gcc



#再次执行报错 

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.


#解决:

[root@qls nginx-1.18.0]# yum install  -y  pcre  pcre-devel 


#再次执行报错

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.


#解决:

[root@qls nginx-1.18.0]# yum  install  -y  openssl  openssl-devel


#4. 编译

[root@qls nginx-1.18.0]# make


5. 将编译的结果拷贝到指定的位置 

[root@qls nginx-1.18.0]# make  install


[root@qls nginx-1.18.0]# ll /opt/
drwxr-xr-x 6 root root   54 Jul 31 16:54 nginx-1.18.0


6. 做个软链接

[root@qls ~]# ln -s  /opt/nginx-1.18.0/  /opt/nginx


7. 启动 

[root@qls ~]# /opt/nginx/sbin/nginx 

2. 定时任务的概念

	设定指定的时间周期性执行你的计划或者任务 
	
	crond		# 守护进程       分钟级别  
	
	两种:
	
		系统级别定时任务:	定时清理文件   收集系统信息  定时切割日志 
		
		用户级别定时任务:	同步时间    定时备份数据 
	

3. 定时任务相关介绍


[root@qls ~]# ll  /etc/cron*  -d
drwxr-xr-x. 2 root root  21 Jul 29 08:56 /etc/cron.d		#定时任务的统一存放目录 
drwxr-xr-x. 2 root root  57 Jul 29 08:56 /etc/cron.daily	#系统每天执行的定时任务
-rw-------  1 root root   0 Apr 11  2018 /etc/cron.deny		#定时任务的黑名单 
drwxr-xr-x. 2 root root  22 Jul 29 08:56 /etc/cron.hourly	#系统每小时执行的定时任务
drwxr-xr-x. 2 root root   6 Jun 10  2014 /etc/cron.monthly	#系统每月执行的定时任务
-rw-r--r--  1 root root 451 Jun 10  2014 /etc/crontab		#定时任务主配置文件
drwxr-xr-x. 2 root root   6 Jun 10  2014 /etc/cron.weekly	#系统每周执行的定时任务 



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

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


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


怎样书写定时任务 


crontab			#书写定时任务的命令  

选项:

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

1. 语法检查 

2. 方便简单 


#定时任务的规则 

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

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

/5	

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

1,3		#不连续的时间  1点和3点 


00 02 * * *   	#每天的凌晨2点整  

00 02 1 * * 	#每个月的1号凌晨2点整  

00 02 14 2 * 	#每年的2月14日凌晨2点整   

00 02 * * 7 	#每周日的凌晨2点整  

00 02 * 6 5 	#每年的6月份的每周五的凌晨2点整    

00 02 14 * 7 	#每个月的14号或者周日的凌晨2点整

00 02 14 2 7  	#每年的2月份的14号或者周日的凌晨2点整

*/10  02 * * * 	#每天的凌晨2点每隔10分钟  

* * * * *   	#每分钟 

00 00 14 2 *  	#每年的2月份14号的凌晨0点整

*/5 * * * *   	#每隔5分钟 

00 02 * 1,5,8 *  #每年的1和5和8月的每天的凌晨2点整

00 02 1-8 * *   #每个月的1到8号的凌晨2点整

00 21 * * *   	#每天晚上21点整

45 4 1,10,22 * * 	#每个月的1,10,22号 的凌晨4点45分 

45 4 1-10 * *  		#每个月的1到10号的凌晨4点45分

3,15 8-11 */2 * * 	#每个月每隔两天的8到11点的3分和15分的时候 

0 23-7/2 * * *   	#每天的23点到7点的每隔2个小时的整点 

15 21 * * 1-5		#每周一到周五的晚上21点15分


4. 定时任务案例

1. 定时同步系统时间 每分钟同步 

[root@qls ~]# ntpdate  ntp.aliyun.com
31 Jul 10:27:12 ntpdate[13673]: step time server 203.107.6.88 offset -28797.933639 sec
[root@qls ~]# date
Fri Jul 31 10:27:18 CST 2020


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

[root@qls ~]# crontab  -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@qls ~]# crontab  -l
#同步系统时间 qls 20200731_10
* * * * *  ntpdate  ntp.aliyun.com

#修改时间
[root@qls ~]# date -s  20200730
Thu Jul 30 00:00:00 CST 2020
[root@qls ~]# date
Thu Jul 30 00:00:02 CST 2020

#查看定时任务的执行过程

[root@qls ~]# tailf  /var/log/cron
Jul 31 17:01:01 qls run-parts(/etc/cron.hourly)[13622]: finished 0anacron
Jul 31 17:52:01 qls crontab[13651]: (root) LIST (root)
Jul 31 18:01:01 qls CROND[13656]: (root) CMD (run-parts /etc/cron.hourly)
Jul 31 18:01:01 qls run-parts(/etc/cron.hourly)[13656]: starting 0anacron
Jul 31 18:01:01 qls run-parts(/etc/cron.hourly)[13665]: finished 0anacron
Jul 31 10:27:49 qls crontab[13675]: (root) BEGIN EDIT (root)
Jul 31 10:30:30 qls crontab[13675]: (root) REPLACE (root)
Jul 31 10:30:30 qls crontab[13675]: (root) END EDIT (root)
Jul 31 10:30:36 qls crontab[13677]: (root) LIST (root)
Jul 30 00:00:03 qls CROND[13682]: (root) CMD (ntpdate  ntp.aliyun.com)

#查看接收的邮件发现了报错  说命令找不到 

[root@qls ~]# ll  /var/spool/mail/root 
-rw------- 1 root mail 3541 Jul 30 00:01 /var/spool/mail/root
[root@qls ~]# tailf /var/spool/mail/root
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20200729160201.064E8802C9EE@qls.localdomain>
Date: Thu, 30 Jul 2020 00:02:01 +0800 (CST)

/bin/sh: ntpdate: command not found


#重新编写定时任务 

[root@qls ~]# crontab -l
#同步系统时间 qls 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com


[root@qls ~]# tailf  /var/log/cron
Jul 30 00:01:01 qls run-parts(/etc/cron.hourly)[13694]: starting 0anacron
Jul 30 00:01:02 qls anacron[13707]: Anacron started on 2020-07-30
Jul 30 00:01:02 qls anacron[13707]: Normal exit (0 jobs run)
Jul 30 00:01:02 qls run-parts(/etc/cron.hourly)[13709]: finished 0anacron
Jul 30 00:02:01 qls CROND[13714]: (root) CMD (ntpdate  ntp.aliyun.com)
Jul 30 00:03:01 qls CROND[13722]: (root) CMD (ntpdate  ntp.aliyun.com)
Jul 30 00:03:31 qls crontab[13728]: (root) BEGIN EDIT (root)
Jul 30 00:03:38 qls crontab[13728]: (root) REPLACE (root)
Jul 30 00:03:38 qls crontab[13728]: (root) END EDIT (root)
Jul 30 00:03:43 qls crontab[13730]: (root) LIST (root)



Jul 30 00:04:01 qls crond[6003]: (root) RELOAD (/var/spool/cron/root)
Jul 30 00:04:01 qls CROND[13734]: (root) CMD (/usr/sbin/ntpdate  ntp.aliyun.com)
Jul 31 10:35:04 qls CROND[13741]: (root) CMD (/usr/sbin/ntpdate  ntp.aliyun.com)

#邮件正在一直接收信息 导致邮件过大 

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

#停掉邮件服务 日志不在发生变化 

[root@qls ~]# systemctl  stop postfix
[root@qls ~]# ll /var/spool/mail/root 
-rw------- 1 root mail 8829 Jul 31 10:37 /var/spool/mail/root

#但是会一直生成小文件 

[root@qls ~]# ll /var/spool/postfix/maildrop/
total 12
-rwxr--r-- 1 root postdrop 601 Jul 31 10:38 7FF40C0CD48D
-rwxr--r-- 1 root postdrop 600 Jul 31 10:39 A8919C0CD48E
-rwxr--r-- 1 root postdrop 601 Jul 31 10:40 CD943C0CD48F


#重新编写定时任务

[root@qls ~]# crontab  -l
#同步系统时间 qls 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null

[root@qls ~]# systemctl  start postfix

[root@qls ~]# ll /var/spool/mail/root 
-rw------- 1 root mail 13469 Jul 31 10:43 /var/spool/mail/root


总结:

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

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

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

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

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


2. 把系统的时间追加到一个文件中


[root@qls ~]# date +%F_%T >> /root/time.txt
[root@qls ~]# cat /root/time.txt
2020-07-31_10:51:45


[root@qls ~]# crontab -l
#同步系统时间 qls 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
#xxxxxxxxxx
* * * * *  /usr/bin/date +%F_%T >> /root/time.txt  


[root@qls ~]# tailf  /var/log/cron
Jul 31 10:54:01 qls crond[6003]: (root) RELOAD (/var/spool/cron/root)
Jul 31 10:54:01 qls CROND[14046]: (root) CMD (/usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null)
Jul 31 10:54:01 qls CROND[14047]: (root) CMD (/usr/bin/date +)


#修改定时任务

[root@qls ~]# crontab -l
#同步系统时间 qls 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
#xxxxxxxxxx
* * * * *  /usr/bin/date +\%F_\%T >> /root/time.txt  



[root@qls ~]# tailf  /var/log/cron
Jul 31 10:56:01 qls crond[6003]: (root) RELOAD (/var/spool/cron/root)
Jul 31 10:56:01 qls CROND[14074]: (root) CMD (/usr/bin/date +%F_%T >> /root/time.txt  )


[root@qls ~]# cat time.txt 
2020-07-31_10:56:01


总结:

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

3. 备份/etc/目录   压缩包名带有时间戳    保留最近的3天数据 

[root@qls ~]# cat backup.sh
#!/bin/bash
#重新定义环境变量 
export  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/.local/bin:/root/bin
#1.创建备份目录
mkdir -p  /backup
#2.开始备份
cd  /
tar czf  /backup/etc_$(date +%F_%M).tar.gz  etc/  
#3.删除3天以前的数据
find  /backup  -type f -mtime  +3   -name "*.tar.gz"  -delete


[root@qls ~]# sh  backup.sh
[root@qls ~]# ll /backup/
total 9972
-rw-r--r-- 1 root root 10210944 Jul 31 11:05 etc_2020-07-31_05.tar.gz

#批量执行 

[root@qls ~]# for i in {20..31};do date -s 2020/07/$i &&  sh /root/backup.sh ;done


#编写定时任务 
[root@qls ~]# crontab  -l
#同步系统时间 qls 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
#xxxxxxxxxx
* * * * *  /usr/bin/date +\%F_\%T >> /root/time.txt  
#备份
* * * * *  /bin/bash   /root/backup.sh  &>/dev/null


[root@qls ~]# tailf  /var/log/cron
Jul 31 11:10:01 qls crond[6003]: (root) RELOAD (/var/spool/cron/root)
Jul 31 11:10:01 qls CROND[14327]: (root) CMD (/usr/bin/date +%F_%T >> /root/time.txt  )
Jul 31 11:10:01 qls CROND[14328]: (root) CMD (/bin/bash   /root/backup.sh  &>/dev/null)
Jul 31 11:10:01 qls CROND[14329]: (root) CMD (/usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null)


[root@qls ~]# ll  /backup/
total 59832
-rw-r--r-- 1 root root 10210944 Jul 28 00:00 etc_2020-07-28_00.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 29 00:00 etc_2020-07-29_00.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 30 00:00 etc_2020-07-30_00.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 31 00:00 etc_2020-07-31_00.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 31 11:05 etc_2020-07-31_05.tar.gz
-rw-r--r-- 1 root root 10210944 Jul 31 11:10 etc_2020-07-31_10.tar.gz

5. 定时发邮件


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

[root@qls ~]# vim /etc/mail.rc

#发件人

set from=1176494252@qq.com
#邮件服务器

set smtp=smtp.qq.com
#发件人用户名

set smtp-auth-user=1176494252@qq.com
#发件人密码(QQ邮箱不可以使用密码,只能使用授权码)

set smtp-auth-password=xxx
#登录方式

set smtp-auth=login
#邮件服务器协议及端口

set smtp=smtps://smtp.qq.com:465
#忽略证书

set ssl-verify=ignore
#指定证书位置

set nss-config-dir=/etc/pki/nssdb/

#或者指定别的证书位置,创建证书目录



#放到最后  
set from=xxx@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=xxx@qq.com
set smtp-auth-password=xxx		#客户端的授权码  
set smtp-auth=login
set smtp=smtps://smtp.qq.com:465
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/


[root@qls ~]# echo  "test"  | mail  -s "hello"  xxxx@qq.com
[root@qls ~]# Error in certificate: Peer's certificate issuer has been marked as not trusted by the.		#忽略这个错误 


[root@qls ~]# mkdir qingshu  
[root@qls ~]# cd qingshu
[root@qls qingshu]# vim  qingshu1.txt
[root@qls qingshu]# mail  -s "致亲爱的小姐姐"   xxx@qq.com  <  qingshu1.txt 
[root@qls qingshu]# Error in certificate: Peer's certificate issuer has been marked as not trusted by the.


#编写自动化脚本 

[root@qls qingshu]# cat /root/send_mail.sh
#!/bin/bash
Qingshu=$(ls -1  /root/qingshu/ |head -1)
mail  -s "致亲爱的小姐姐"  xxxx@qq.com  < /root/qingshu/$Qingshu  
mail  -s "致亲爱的小姐姐"  xxxx@qq.com  < /root/qingshu/$Qingshu

if  [ $? -eq 0 ];then
	rm -f /root/qingshu/$Qingshu
fi


[root@qls qingshu]# ll
total 20
-rw-r--r-- 1 root root  85 Jul 31 11:49 qingshu1.txt
-rw-r--r-- 1 root root 184 Jul 31 11:51 qingshu2.txt
-rw-r--r-- 1 root root 121 Jul 31 11:51 qingshu3.txt
-rw-r--r-- 1 root root 155 Jul 31 11:51 qingshu4.txt
-rw-r--r-- 1 root root  70 Jul 31 11:52 qingshu5.txt


#编写定时任务 

[root@qls qingshu]# crontab  -l
#同步系统时间 qls 20200731_10
* * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
#xxxxxxxxxx
#* * * * *  /usr/bin/date +\%F_\%T >> /root/time.txt  
#备份
#* * * * *  /bin/bash   /root/backup.sh  &>/dev/null
#xxxxxxxxx
* * * * *  /bin/bash   /root/send_mail.sh  &>/dev/null


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值