1. 源码包管理
1. 要有源码包 下载源码包
[ root@qls ~]
[ root@qls ~]
total 1016
-rw-r--r-- 1 root root 1039530 Apr 21 22:33 nginx-1.18.0.tar.gz
2. 解压软件包
[ root@qls ~]
[ root@qls ~]
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]
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
[ root@qls nginx-1.18.0]
./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]
./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]
[ root@qls nginx-1.18.0]
5. 将编译的结果拷贝到指定的位置
[ root@qls nginx-1.18.0]
[ root@qls nginx-1.18.0]
drwxr-xr-x 6 root root 54 Jul 31 16:54 nginx-1.18.0
6. 做个软链接
[ root@qls ~]
7. 启动
[ root@qls ~]
2. 定时任务的概念
设定指定的时间周期性执行你的计划或者任务
crond
两种:
系统级别定时任务: 定时清理文件 收集系统信息 定时切割日志
用户级别定时任务: 同步时间 定时备份数据
3. 定时任务相关介绍
[ root@qls ~]
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 ~]
SHELL= /bin/bash
PATH= /sbin:/bin:/usr/sbin:/usr/bin
MAILTO= root
分时日月周 用户 命令
[ root@qls ~]
total 0
[ root@qls ~]
[ root@qls ~]
怎样书写定时任务
crontab
选项:
-e
-l
1. 语法检查
2. 方便简单
*
*/5
/5
1-3
1,3
00 02 * * *
00 02 1 * *
00 02 14 2 *
00 02 * * 7
00 02 * 6 5
00 02 14 * 7
00 02 14 2 7
*/10 02 * * *
* * * * *
00 00 14 2 *
*/5 * * * *
00 02 * 1,5,8 *
00 02 1-8 * *
00 21 * * *
45 4 1,10,22 * *
45 4 1-10 * *
3,15 8-11 */2 * *
0 23-7/2 * * *
15 21 * * 1-5
4. 定时任务案例
1. 定时同步系统时间 每分钟同步
[ root@qls ~]
31 Jul 10:27:12 ntpdate[ 13673] : step time server 203.107.6.88 offset -28797.933639 sec
[ root@qls ~]
Fri Jul 31 10:27:18 CST 2020
定时任务最好加上注释 作者 时间
[ root@qls ~]
no crontab for root - using an empty one
crontab: installing new crontab
[ root@qls ~]
* * * * * ntpdate ntp.aliyun.com
[ root@qls ~]
Thu Jul 30 00:00:00 CST 2020
[ root@qls ~]
Thu Jul 30 00:00:02 CST 2020
[ root@qls ~]
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 ~]
-rw------- 1 root mail 3541 Jul 30 00:01 /var/spool/mail/root
[ root@qls ~]
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 ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com
[ root@qls ~]
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 ~]
-rw------- 1 root mail 7028 Jul 31 10:35 /var/spool/mail/root
[ root@qls ~]
-rw------- 1 root mail 7929 Jul 31 10:36 /var/spool/mail/root
[ root@qls ~]
[ root@qls ~]
-rw------- 1 root mail 8829 Jul 31 10:37 /var/spool/mail/root
[ root@qls ~]
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 ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
[ root@qls ~]
[ root@qls ~]
-rw------- 1 root mail 13469 Jul 31 10:43 /var/spool/mail/root
总结:
1. 定时任务要有注释 作者 时间
2. 定时任务的命令一定要在命令行上面执行成功
3. 定时任务要使用绝对路径
4. 定时任务写命令的时候,尽量复制之前执行成功的命令 减少出错率
5. 定时任务的执行结果定向到指定的文件中或者定向到空
2. 把系统的时间追加到一个文件中
[ root@qls ~]
[ root@qls ~]
2020-07-31_10:51:45
[ root@qls ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
* * * * * /usr/bin/date +%F_%T >> /root/time.txt
[ root@qls ~]
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 ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
* * * * * /usr/bin/date +\%F_\%T >> /root/time.txt
[ root@qls ~]
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 ~]
2020-07-31_10:56:01
总结:
定时任务中,有些特殊字符不识别,需要转义
3. 备份/etc/目录 压缩包名带有时间戳 保留最近的3天数据
[ root@qls ~]
export PATH= /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/.local/bin:/root/bin
mkdir -p /backup
cd /
tar czf /backup/etc_$( date +%F_%M) .tar.gz etc/
find /backup -type f -mtime +3 -name "*.tar.gz" -delete
[ root@qls ~]
[ root@qls ~]
total 9972
-rw-r--r-- 1 root root 10210944 Jul 31 11:05 etc_2020-07-31_05.tar.gz
[ root@qls ~]
[ root@qls ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
* * * * * /usr/bin/date +\%F_\%T >> /root/time.txt
* * * * * /bin/bash /root/backup.sh & > /dev/null
[ root@qls ~]
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 ~]
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 ~]
[ root@qls ~]
set from= 1176494252@qq.com
set smtp= smtp.qq.com
set smtp-auth-user= 1176494252@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/
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 ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls qingshu]
[ root@qls qingshu]
[ root@qls qingshu]
[ root@qls qingshu]
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]
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]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
* * * * * /bin/bash /root/send_mail.sh & > /dev/null