@linux定时任务介绍及使用
2|01. 源码包管理
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
3|02. 定时任务的概念
设定指定的时间周期性执行你的计划或者任务
crond # 守护进程 分钟级别
两种:
系统级别定时任务: 定时清理文件 收集系统信息 定时切割日志
用户级别定时任务: 同步时间 定时备份数据
4|03. 定时任务相关介绍
[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分
5|04. 定时任务案例
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
6|05. 定时发邮件
[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
__EOF__
本文作者:ଲ小何才露煎煎饺
本文链接:https://www.cnblogs.com/zeny/p/15121631.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/zeny/p/15121631.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文来自博客园,作者:ଲ小何才露煎煎饺,转载请注明原文链接:https://www.cnblogs.com/zeny/p/15121631.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报