Linux-07
定时任务
crond
系统级别 :定时文件清理,日志切割,定时收集系统的状态
用户级别 :同步系统时间,定时备份数据
定时任务相关的文件
[ root@qls ~]
[ root@qls ~]
drwxr-xr-x. 2 root root 21 Aug 14 15:11 /etc/cron.d
drwxr-xr-x. 2 root root 42 Aug 14 15:12 /etc/cron.daily
-rw-------. 1 root root 0 Apr 11 2018 /etc/cron.deny
drwxr-xr-x. 2 root root 22 Aug 14 15:11 /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
/var/spool/cron/
[ root@qls ~]
[ root@qls ~]
SHELL= /bin/bash
PATH= /sbin:/bin:/usr/sbin:/usr/bin
MAILTO= root
*
-
,
*/5
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
crontab
选项
-e
-l
-r
-u
每分钟定时执行同步系统时间
[ root@qls ~]
[ root@qls ~]
Wed Aug 21 09:17:11 CST 2019
[ root@qls ~]
21 Aug 09:17:59 ntpdate[ 8071] : adjust time server 203.107.6.88 offset 0.031011 sec
[ root@qls ~]
Wed Aug 21 09:18:04 CST 2019
[ root@qls ~]
Thu Aug 1 00:00:00 CST 2019
[ root@qls ~]
21 Aug 09:18:39 ntpdate[ 8074] : step time server 203.107.6.88 offset 1761503.866272 sec
[ root@qls ~]
Wed Aug 21 09:18:45 CST 2019
[ root@qls ~]
* * * * * ntpdate ntp.aliyun.com
[ root@qls ~]
Aug 21 09:20:43 qls crontab[ 8077] : ( root) REPLACE ( root)
Aug 21 09:20:43 qls crontab[ 8077] : ( root) END EDIT ( root)
Aug 21 09:20:49 qls crontab[ 8080] : ( root) LIST ( root)
Aug 21 09:21:01 qls CROND[ 8083] : ( root) CMD ( ntpdate ntp.aliyun.com)
Aug 1 00:00:47 qls CROND[ 8095] : ( root) CMD ( ntpdate ntp.aliyun.com)
[ root@qls ~]
Thu Aug 1 00:00:58 CST 2019
[ 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: < 20190731160201.2D69620C1941@qls.localdomain>
Date: Thu, 1 Aug 2019 00:02:01 +0800 ( CST)
/bin/sh: ntpdate: command not found
[ root@qls ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com
[ root@qls ~]
Wed Aug 21 09:26:32 CST 2019
[ root@qls ~]
-rw-------. 1 root mail 10447 Aug 21 09:30 /var/mail/root
You have new mail in /var/spool/mail/root
[ root@qls ~]
[ root@qls ~]
total 12
-rwxr--r--. 1 root postdrop 600 Aug 21 09:32 2250C400DE75
-rwxr--r--. 1 root postdrop 599 Aug 21 09:33 4D709400DE76
-rwxr--r--. 1 root postdrop 599 Aug 21 09:31 E884E400DE61
[ root@qls ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
[ root@qls ~]
[ root@qls ~]
-rw-------. 1 root mail 14945 Aug 21 09:36 /var/mail/root
总结:
有注释
使用绝对路径
把结果追加到空或者追加到文件中
定时向一个文件追加时间信息
[ root@qls ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
* * * * * /usr/bin/date +%F_%T >> /root/time.txt
[ root@qls ~]
Aug 21 10:03:01 qls crond[ 7032] : ( root) RELOAD ( /var/spool/cron/root)
Aug 21 10:03:01 qls CROND[ 8583] : ( root) CMD ( /usr/bin/date +)
Aug 21 10:03:01 qls CROND[ 8586] : ( root) CMD ( /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null )
[ root@qls ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
* * * * * /usr/bin/date +\%F_\%T >> /root/time.txt
[ root@qls ~]
Aug 21 10:05:01 qls crond[ 7032] : ( root) RELOAD ( /var/spool/cron/root)
Aug 21 10:05:01 qls CROND[ 8614] : ( root) CMD ( /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null )
Aug 21 10:05:01 qls CROND[ 8615] : ( root) CMD ( /usr/bin/date +%F_%T >> /root/time.txt )
总结:
定时任务中,有些特殊符号,比如%,会不被识别
定时备份数据
要求
备份 /etc/
etc_2019-08-21_10-09.tar.gz
var_2019-08-21_10-09.tar.gz
统一存放一个备份目录
删除三天前的备份数据
[ root@qls ~]
Time= $( date +%F_%H-%M)
mkdir -p /backup
cd /
tar czPf /backup/etc_${Time} .tar.gz etc/
find /backup -type f -mtime +3 -delete
[ root@qls ~]
[ root@qls ~]
* * * * * /usr/sbin/ntpdate ntp.aliyun.com & > /dev/null
* * * * * /usr/bin/date +\%F_\%T >> /root/time.txt
* * * * * /bin/sh /root/back.sh & > /dev/null
[ root@qls ~]
total 9980
-rw-r--r--. 1 root root 10215613 Aug 21 10:25 etc_2019-08-21_10-25.tar.gz
[ root@qls ~]
export PATH= /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Time= $( date +%F_%H-%M)
mkdir -p /backup
cd /
tar czPf /backup/etc_${Time} .tar.gz etc/
find /backup -type f -mtime +3 -delete
定时任务怎么备份
/var/spool/cron/root
如何禁止一个用户使用定时任务
echo 'oldboy' >>/etc/cron.deny
练习题
1.每分钟打印你的名字到oldboy.txt中。
* * * * * command
2.每周六的早上8点到12点,执行/scripts/test.sh脚本。
00 8-12 * * 6 command
3.每个月的1号,执行/scripts/oldboy.sh脚本
00 01 1 * * command
4.每年的10月份1号到7号,执行/scripts/holiday.sh脚本
00 01 1-7 10 * command
5.每天的9点和11点,执行/scripts/test.sh脚本
00 9,11 * * * command
定时给小姐姐发情书
yum install -y mailx
进程管理
[ root@qls ~]
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
第一列:进程运行的用户
第二列:进程pid号码
第三列:占用CPU的百分比
第四列: 占用内存的百分比
第五列: 占用虚拟内存
第六列: 占用的物理内存
第七列:终端
第八列:状态
第九列:进程启动时间
第十列:进程占用CPU的时间
第11列: 执行的命令
[ root@qls ~]
top - 11:46:57 up 3:14, 2 users, load average: 0.68, 0.24, 0.12
Tasks: 106 total, 1 running, 105 sleeping, 0 stopped, 0 zombie
%Cpu( s) : 0.0 us, 5.4 sy, 0.0 ni, 94.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2028088 total, 1097584 free, 113832 used, 816672 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1709956 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
第一行: 11:46:57 当前系统时间 3:14, 当前系统运行时间 2 users 当前登录的用户数量
第二行:任务,总任务的数量,
关于进程的命令
ps
top
htop
glances
iotop
iostat
netstat -lntp
ss
[ root@qls ~]
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.16.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
[ root@qls ~]
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.2 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
172.16.1.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
进程管理
[ root@qls ~]
如果进程不存在,会提示
[ root@qls ~]
如果进程不存在,会提示
[ root@qls ~]
没有任何提示
后台管理
screen
选项
-S
-r
ctrl键a +d 临时退出
SSH
1. 远程连接
2. 数据加密传输
Telnet 不加密 tcp/23 不支持root用户登录
ssh 加密 tcp/22 支持root用户登录
测试使用Telnet
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
Changing password for user test.
passwd: all authentication tokens updated successfully.
[ root@qls ~]
Trying 10.0.0.100.. .
Connected to 10.0.0.100.
Escape character is '^]' .
Kernel 3.10.0-957.el7.x86_64 on an x86_64
qls login: root
Password:
Login incorrect
qls login: root
Password:
Login incorrect
qls login: test
Password:
Last login: Wed Aug 21 14:53:34 from ::ffff:10.0.0.1
[ test@qls ~] $
ssh相关的命令
scp
ssh
ssh-keygen
ssh-copy-id
[ root@qls ~]
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:UJfXPNHA1tsUI9NsyZWwf2/IF/NcEErY/iJ74f4NyR4.
ECDSA key fingerprint is MD5:c9:76:b4:5e:4f:51:5a:00:e0:ba:f0:d6:c5:17:b9:b5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ' 10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7' s password:
nginx-1.16.1.tar.gz
[ root@qls ~]
total 1016
-rw-------. 1 root root 1614 Aug 14 15:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 1032630 Aug 14 01:01 nginx-1.16.1.tar.gz
[ root@qls ~]
[ root@qls ~]
root@10.0.0.7's password:
nginx-1.16.1.tar.gz 100% 1008KB 92.8MB/s 00:00
#限速传输, 单位kb
[root@qls ~]# scp -rp -l 8096 swap.txt root@10.0.0.7:/root
#修改主机名
[root@qls ~]# hostnamectl set-hostname web01
[root@qls ~]# bash
[root@web01 ~]#
[root@web01 ~]#
[root@web01 ~]# cat /etc/hostname
web01
#命令行连接
[root@qls ~]# ssh root@10.0.0.100 -p22
#本地连接
[C:\~]$ ssh root@10.0.0.100 22
#远程执行命令
[root@web01 ~]# ssh root@10.0.0.100 "ifconfig eth0"
root@10.0.0.100' s password:
eth0: flags= 4163< UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::52f8:a673:eea3:dc47 prefixlen 64 scopeid 0x20< link>
ether 00:0c:29:0d:42:09 txqueuelen 1000 ( Ethernet)
RX packets 47639 bytes 43560570 ( 41.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 70530 bytes 131220281 ( 125.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
基于秘钥方式连接
[ root@qls ~]
Generating public/private rsa key pair.
Enter file in which to save the key ( /root/.ssh/id_rsa) :
Enter passphrase ( empty for no passphrase) :
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:K/fUKgI3bNlQKbirzmPz3+UFWgHcr+j2OAiDBh8UghI root@qls
The key's randomart image is:
+---[RSA 2048]----+
|E... . ..o |
|o.. . . +.. |
|.. . o .. |
|. . . . .. |
| o o o +S.o. |
| + = * o+.o |
| . . *.++ o o |
| .= o+=* o |
| oo+...oo+= |
+----[SHA256]-----+
#2.分发公钥
[root@qls ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.7' s password:
Number of key( s) added: 1
Now try logging into the machine, with: "ssh 'root@10.0.0.7'"
and check to make sure that only the key( s) you wanted were added.
[ root@qls ~]
Last login: Wed Aug 21 15:21:32 2019 from 10.0.0.1
[ root@web01 ~]
ssh优化
Port 6666
PermitRootLogin no
PasswordAuthentication no
UseDNS no
GSSAPIAuthentication no