day21
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
6. day19作业讲解
1.linux下常见的压缩包类型有哪些
2.将/etc/hosts文件用tar格式打包。
tar czf hosts.tar.gz /etc/hosts
3.查看打包之后的/etc/hosts的文件内容,在不解压的情况下查看。
tar tf hosts.tar.gz
4.使用tar打包/var/log/目录。
tar czf log.tar.gz /var/log/
5.使用zip打包/etc目录。
zip -r etc.zip /etc
6.查看/var/log/目录的压缩包中有哪些内容。
tar tf log.tar.gz
7.将/var/log/目录解压到/opt目录中。
tar xf log.tar.gz -C /opt
10.解压/etc/目录到/opt目录中。
unzip etc.zip -d /opt
11.用zip打包/opt目录,要求不显示打包过程。
zip -rq opt.zip /opt
12.打包/etc/目录,要求是.bz2格式
tar cjf etc.tar.bz2 /etc/
13.打包/var/log目录,要求是.xz格式
tar cJf log.tar.xz /var/log
14.使用tar命令打包/etc/时,会出现一个删根的操作,怎样打包不会进行删根的操作
tar czPf etc.tar.gz /etc
cd /
tar czf /root/etc.tar.gz /etc
15.打包/etc/目录,要求不打包/etc/hosts这个文件。
tar czf etc.tar.gz --exclude= /etc/hosts /etc/
16.打包/etc/目录,要求不打包/etc/hosts和/etc/hostname这两个文件。
tar czf etc.tar.gz --exclude= /etc/{ hosts,hostname} /etc/
17.打包/etc/目录,但要排除passwd,shadow,group,gshadow,hosts,hostname这些文件。( 你能用两种方法实现吗)
vim paichu.list
/etc/passwd
/etc/shadow
/etc/group
/etc/gshadow
/etc/hosts
/etc/hostname
tar czfX etc.tar.gz paichu.list /etc
18.已知/etc/grub2.cfg文件是个软连接文件,在你不知道的情况下,请问怎么打包该文件的真实文件。
tar czhf grub.tar.gz /etc/grub2.cfg
19.把/var/log/目录中所有.log的文件进行打包成一个压缩包,名称定义为log.tar.gz的压缩包。
find /var/log -type f -name "*.log" | xrags tar czf log.tar.gz
20.已知文件oldboy.gz,请问在不解压的情况下,怎样查看该文件的内容。
zcat oldboy.gz
21.打包/etc/目录,命令以ip地址+当前时间方式的压缩包:比如: 10.0.0.100_2019-12-24_etc.tar.gz
tar czf $( ifconfig eth0 | awk 'NR==2{print $2 }' ) _$( date +%F) _etc.tar.gz /etc
22.创建/data/bak目录,然后复制如下文件到/data/bak目录下
/etc/hosts
/etc/resolv.conf
/etc/fstab
/etc/bashrc
/etc/profile
/etc/rc.local
/etc/sudoers
23.接22题,使用tar命令对/data/bak目录下的文件及目录以gzip的格式进行归档压缩到/data目录下(压缩包的名字以自己名字命名)
tar czf /data/qls.tar.gz /data/bak
24.使用tar命令查看上题/data目录下压缩包内的内容。
tar tf /data/qls.tar.gz
25.把第23题/data目录下的压缩包,解压到/backup目录下
mkdir /backup
tar xf /data/qls.tar.gz -C /backup
26.再次使用tar命令把/data/bak目录下的文件及目录以gzip的格式进行归档压缩到/data目录下,但是在进行归档压缩时,排除文件“sudoers”,然后查看该压缩包内容是否存在文件“sudoers”(压缩包名自行拟定)
tar czf /data/qls.tar.gz --exclude= /data/bak/sudoers /data/bak
27.打包/etc目录下所有普通文件到root用户家目录。
find /etc/ -type f | xargs tar czf /root/file.tar.gz
28.打包/etc/目录到/opt/目录下,名称要求以当前主机名和ip地址命名,例:oldboy_10.0.0.100.tar.gz
tar czf /opt/$( hostname ) _$( date +%F) _etc.tar.gz /etc
29.如何使用gzip命令对文件进行压缩、解压
gzip file.txt
gzip -d file.txt.gz
30.如何用zip命令对文件以及目录进行压缩、解压
zip file.txt.zip file.txt
zip -r etc.zip /etc
unzip etc.zip
unzip etc.zip -d /opt
31.创建一个自己名字的文件至/opt目录
32.打包opt整个目录,并命名test_opt.tar.gz
tar czf test_opt.tar.gz /opt
33.查看打包好的test_opt.tar.gz里的文件
tar tf test_opt.tar.gz
34.将打包好的test_opt.tar.gz内容指定解压至/tmp目录
tar xf test_opt.tar.gz -C /tmp
35.打包etc目录下的所有文件,不要目录只要文件
find /etc ! -type d -exec cp { } /opt/ \;
tar czf opt.tar.gz /opt
find /etc ! -type d | xargs tar czf file.tar.gz
36.打包etc目录下的所有文件,排除passwd,shadow
tar czf etc.tar.gz --exclude= /etc/{ passwd,shadow} /etc
37.打包etc目录下的所有以p开头的文件
find /etc/ -type f -name "p*" | xargs tar czf p.tar.gz
38.打包etc目录下所有大于1M的文件
find /etc/ -type f -size +1M | xargs tar czf big_file.tar.gz