zoukankan      html  css  js  c++  java
  • 7

    Linux-day07

    定时任务

    crond
    
    
    系统级别	:定时文件清理,日志切割,定时收集系统的状态
    用户级别	:同步系统时间,定时备份数据
    
    

    定时任务相关的文件

    [root@qls ~]# 
    [root@qls ~]# ll  /etc/cron*  -d
    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 ~]# ll /var/log/cron		#定时任务日志
    
    
    [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
    
    *	#所有,每
    -	#连续的时间,1-5
    ,	#不连续的时间,1,5
    */5  #每个5个单位
    
    
    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点每隔十分钟执行一次   
    ​
    * * * * * 		# 每分钟执行一次       
    ​
    00 00 14 2 *	# 每年的2月份14号凌晨12点整执行一次     
    ​
    */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		#编辑定时任务 vim  /var/spool/cron/username
    	-l		#查看定时任务 cat
    	-r		#删除定时任务文件
    	-u		#指定用户,默认是当前用户
    
    

    每分钟定时执行同步系统时间

    #安装软件
    [root@qls ~]# yum install  ntpdate  -y
    
    #命令行测试是否可以同步时间
    [root@qls ~]# date
    Wed Aug 21 09:17:11 CST 2019
    [root@qls ~]# ntpdate  ntp.aliyun.com
    21 Aug 09:17:59 ntpdate[8071]: adjust time server 203.107.6.88 offset 0.031011 sec
    [root@qls ~]# date
    Wed Aug 21 09:18:04 CST 2019
    [root@qls ~]# date  -s  2019/08/01
    Thu Aug  1 00:00:00 CST 2019
    [root@qls ~]# ntpdate  ntp.aliyun.com
    21 Aug 09:18:39 ntpdate[8074]: step time server 203.107.6.88 offset 1761503.866272 sec
    [root@qls ~]# date
    Wed Aug 21 09:18:45 CST 2019
    
    #编写定时任务
    [root@qls ~]# crontab  -l
    #sync  time
    * * * * *  ntpdate  ntp.aliyun.com
    
    #查看日志,看是否执行成功
    [root@qls ~]# tailf   /var/log/cron 
    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 ~]# date
    Thu Aug  1 00:00:58 CST 2019
    
    #查看邮件
    [root@qls ~]# tailf  /var/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: <20190731160201.2D69620C1941@qls.localdomain>
    Date: Thu,  1 Aug 2019 00:02:01 +0800 (CST)
    
    /bin/sh: ntpdate: command not found		#发现命令找不到
    
    #重新编写定时任务
    [root@qls ~]# crontab  -e
    #sync  time
    * * * * *  /usr/sbin/ntpdate  ntp.aliyun.com
    
    #再次检查发现,定时任务成功执行
    [root@qls ~]# date
    Wed Aug 21 09:26:32 CST 2019
    
    #但是,系统会发送邮件
    [root@qls ~]# ll  /var/mail/root
    -rw-------. 1 root mail 10447 Aug 21 09:30 /var/mail/root
    You have new mail in /var/spool/mail/root
    
    #停掉邮件服务
    [root@qls ~]# systemctl   stop  postfix
    
    #发现系统生成大量的小文件
    [root@qls ~]# ll  /var/spool/postfix/maildrop/
    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 ~]# crontab  -e
    #sync  time
    * * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null 
    
    #发现系统不再生成小文件
    #启动邮件服务
    [root@qls ~]# systemctl  start  postfix
    
    #邮件也不会再次发送了。
    [root@qls ~]# ll /var/mail/root 
    -rw-------. 1 root mail 14945 Aug 21 09:36 /var/mail/root
    
    
    总结:
    有注释
    使用绝对路径
    把结果追加到空或者追加到文件中
    
    

    定时向一个文件追加时间信息

    [root@qls ~]# crontab  -l
    #sync  time
    * * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null  
    #
    * * * * *  /usr/bin/date +%F_%T  >> /root/time.txt 
    
    #查看执行记录,发现+后面的内容没有识别
    [root@qls ~]# tailf  /var/log/cron 
    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 ~]# crontab  -e
    #sync  time
    * * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
    #
    * * * * *  /usr/bin/date +\%F_\%T  >> /root/time.txt
    
    #定时任务被成功的执行
    [root@qls ~]# tailf  /var/log/cron 
    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 ~]# vim  back.sh
    #!/bin/bash
    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 ~]# for  i in {15..21} ;do  date -s  "2019/08/$i"  &&  sh  back.sh ;done
    
    [root@qls ~]# crontab  -e
    #sync  time
    * * * * *  /usr/sbin/ntpdate  ntp.aliyun.com &>/dev/null
    #
    * * * * *  /usr/bin/date +\%F_\%T  >> /root/time.txt
    #backup /etc
    * * * * * /bin/sh  /root/back.sh &>/dev/null
    
    [root@qls ~]# ll /backup/
    total 9980
    -rw-r--r--. 1 root root 10215613 Aug 21 10:25 etc_2019-08-21_10-25.tar.gz
    
    #脚本中会出现命令找不到的情况
    [root@qls ~]# vim  back.sh 
    #!/bin/bash
    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 ~]# ps  aux  
    USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    
    第一列:进程运行的用户
    
    第二列:进程pid号码
    
    第三列:占用CPU的百分比
    
    第四列: 占用内存的百分比
    
    第五列: 占用虚拟内存
    
    第六列: 占用的物理内存
    
    第七列:终端
    
    第八列:状态
    
    第九列:进程启动时间
    
    第十列:进程占用CPU的时间
    
    第11列: 执行的命令
    
    [root@qls ~]# top
    top - 11:46:57 up  3:14,  2 users,  load average: 0.68, 0.24, 0.12  #系统负载 1  5  15 分钟
    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	#升级版top
     glances	#显示系统性能
     iotop
     iostat
     
     
     netstat   -lntp   #查看端口号
     ss		#用法一样
     
     #查看网关路由
     [root@qls ~]# netstat   -rn
    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 ~]# route -n
    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
    
    #查看tcp的11种状态
    [root@qls ~]# netstat  -ant
    [root@qls ~]# ss -ant
    
    #扫描端口
    [root@qls ~]# nmap -p1-100  10.0.0.100
    
    
    进程管理
    [root@qls ~]# kill 10466		#kill 根据进程pid进行终止进程  -9  强制杀掉  -1 重载
    
    如果进程不存在,会提示
    
    [root@qls ~]# killall  vim		#killall根据进程名称去终止
    
    如果进程不存在,会提示
    
    
    [root@qls ~]# pkill  top		#模糊杀手,包含进程的所有进程都是终止
    没有任何提示
    
    
    

    后台管理

    screen   	#后台管理
    
    选项	
    	-S		#给会话窗口起个命令
    	-r		#进入子窗口
    ctrl键a +d  临时退出
    
    
    

    SSH

    1.  远程连接
    2.  数据加密传输
    
    Telnet		不加密      tcp/23      不支持root用户登录
    ssh			加密		  tcp/22	 支持root用户登录
    
    
    测试使用Telnet
    
    #安装服务端
      [root@qls ~]# yum  install  -y  telnet-server
     
     #启动服务
     [root@qls ~]# systemctl  start telnet.socket
     
     #查看端口号
     [root@qls ~]# netstat   -lntp  |grep  23
     
     #创建测试用户
    [root@qls ~]# useradd     test  
    [root@qls ~]# echo '123'  |passwd   --stdin  test
    Changing password for user test.
    passwd: all authentication tokens updated successfully.
    
    #连接测试
    [root@qls ~]# telnet  10.0.0.100
    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		#远程登录的命令
    
    ssh-keygen	#生成秘钥的命令
    
    ssh-copy-id		#分发公钥的命令
    
    
    #推
    [root@qls ~]# scp  -rp   nginx-1.16.1.tar.gz   root@10.0.0.7:/root
    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 ~]# ll
    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 ~]# scp  -rp  root@10.0.0.100:/root/nginx-1.16.1   /root
    
    #使用远程连接端口号传输
    [root@qls ~]# scp -P22   -rp   nginx-1.16.1.tar.gz   root@10.0.0.7:/root
    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
    
    
    
    

    基于秘钥方式连接

    #1.生成秘钥对
    [root@qls ~]# ssh-keygen   -t  rsa  
    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.
    
    
    #3.连接测试
    [root@qls ~]# ssh  root@10.0.0.7
    Last login: Wed Aug 21 15:21:32 2019 from 10.0.0.1
    [root@web01 ~]# 
    
    
    

    ssh优化

    Port 6666                       # 变更SSH服务远程连接端口
    PermitRootLogin         no      # 禁止root用户直接远程登录
    PasswordAuthentication  no      # 禁止使用密码直接远程登录
    UseDNS                  no      # 禁止ssh进行dns反向解析,影响ssh连接效率参数
    GSSAPIAuthentication    no      # 禁止GSS认证,减少连接时产生的延迟
    
    
    
  • 相关阅读:
    【LeetCode每日一题】2020.6.28 209. 长度最小的子数组
    【《你不知道的JS(中卷①)》】三、原生函数
    【《你不知道的JS(中卷①)》】二、值
    【《你不知道的JS(中卷①)》】一、类型
    【LeetCode每日一题】2020.6.26 面试题 02.01. 移除重复节点
    【LeetCode周赛】第194场周赛
    【LeetCode每日一题】2020.6.25 139. 单词拆分
    ios网络编程(HTTP socket)
    objective c 代码块blocks完整总结二
    objective c 代码块blocks完整总结一
  • 原文地址:https://www.cnblogs.com/chenwang-23/p/11854482.html
Copyright © 2011-2022 走看看