zoukankan      html  css  js  c++  java
  • 【转】Linux中常见问题(磁盘 定时任务)

    【转】Linux中常见问题(磁盘 定时任务)

    第1章 linux无法上网

    1)     第一步,ping域名.

    ping www.baidu.com

    2)ping一个公网ip ,

    ping 223.5.5.5/223.6.6.6/114.114.114.114

    [root@znix /]# ping 223.5.5.5

    PING 223.5.5.5 (223.5.5.5) 56(84) bytes of data.

    64 bytes from 223.5.5.5: icmp_seq=1 ttl=128 time=42.8 ms

    2)     ping 默认的网关

    [root@znix ~]# route -n

    Kernel IP routing table

    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

    10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0

    169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

    0.0.0.0         10.0.0.2        0.0.0.0         UG    0      0        0 eth0

    第2章 系统的负载/cpu/内存太高了

    1)     使用top 命令查看哪个进程占用cpu/内存多.

    [root@znix /]# top

    top - 15:29:37 up 4 days, 23:18,  4 users,  load average: 0.00, 0.00, 0.00

    Tasks:  92 total,   1 running,  91 sleeping,   0 stopped,   0 zombie

    Cpu(s):  0.3%us,  0.3%sy,  0.0%ni, 99.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

    Mem:    485984k total,   215144k used,   270840k free,   117300k buffers

    Swap:   786428k total,     8664k used,   777764k free,    20700k cached

       PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                               

     16949 root      20   0  102m 4628 3572 S  0.0  1.0   0:00.14 sshd                                                                   

     17064 root      20   0  102m 4608 3568 S  0.0  0.9   0:00.06 sshd                                                                   

     16812 root      20   0  102m 2604 2400 S  0.0  0.5   0:00.84 sshd

    2)     找出对应程序的pid号码

    a)       找到占用率较高的程序

    16949 root      20   0  102m 4628 3572 S  0.0  1.0   0:00.14 sshd  

    3)     strace -p  进程的pid号码

    a)       查看现在这个进程正在做什么事情.

    [root@znix /]# strace -p 16949

    Process 16949 attached

    select(10, [3 5 9], [], NULL, NULL

    第3章 定时任务

    3.1 每分钟显示当前系统的日期 --_小时 ,把这个时间追加到 /tmp/date.log

    3.1.1 .先运行命令

    运行命令进行测试,确定命令可以执行.

    [root@znix ~]# date +%F_%H >> /tmp/date.log

    [root@znix ~]# cat /tmp/date.log

    2017-09-08_09

    3.1.2 命令放入脚本中

    [root@znix scripts]# cat /server/scripts/date.sh

    date +%F_%H:%M >> /tmp/date.log

    3.1.3 测试脚本

    [root@znix scripts]# /bin/sh /server/scripts/date.sh

    [root@znix scripts]# tail -1 /tmp/date.log

    2017-09-08_09:43

    3.1.4 写到定时任务

    [root@znix scripts]# crontab -l |tail -2

    #print time to file by hou 20170908

    * * * * * /bin/sh /server/scripts/date.sh  >/dev/null 2>&1

    3.1.5 查看你定时任务的执行日历

    [root@znix scripts]# tail -2 /var/log/cron

    Sep  8 09:53:41 znix crontab[16396]: (root) LIST (root)

    Sep  8 09:54:01 znix CROND[16400]: (root) CMD (/bin/sh /server/scripts/date.sh  >/dev/null 2>&1)

    3.1.6 检查结果

    [root@znix ~]# cat /tmp/date.log

    2017-09-08_09

    3.2 命令中的shbash

    [root@znix ~]# ll /bin/sh /bin/bash

    -rwxr-xr-x. 1 root root 942200 Mar 23 08:15 /bin/bash

    lrwxrwxrwx. 1 root root      4 Aug 10 18:34 /bin/sh -> bash

    第4章 磁盘满了之邮件服务

    4.1 系统中的邮件服务

    centos 6.x postfix

    centos 5.x sendmail

    4.2 关闭系统邮件服务

    [root@znix ~]# /etc/init.d/postfix

    Usage: /etc/init.d/postfix {start|stop|restart|reload|abort|flush|check|status|condrestart}

    [root@znix ~]# /etc/init.d/postfix stop

    Shutting down postfix:                                     [  OK  ]

    4.2.1 关闭邮件服务后,系统的油价会放入/var/spool/postfix/maildrop/

    [root@znix ~]# ls /var/spool/postfix/maildrop/ |wc -l

    12

    4.3 [企业案例] 如果定时任务规则结尾不加>/dev/null 2>&1或者追加到文件中>>/tmp/clsn 2>&1,很容易导致硬盘inode空间被占满,从而系统服务不正常。

    定时任务中-命令或脚本结果(正确及错误)定向到黑洞(>/dev/null 2>&1)或追加到文件中 >>/tmp/clsn.txt 2>&1.

    实例4-1 情况一: 

    邮件服务关闭 没有定向到空,会导致 /var/spool/postfix/maildrop/ 中有许多小文件

    造成磁盘满了,inode满了.

    解决办法:将邮件服务打开就会释放inode.

    [root@znix /]# /etc/init.d/postfix start

    Starting postfix:                                          [  OK  ]

    [root@znix /]# chkconfig |grep post

    postfix        0:off   1:off   2:on    3:on    4:on    5:on    6:off

    实例4-2 情况二:

    邮件服务开启/var/spool/cron/root会变大,发的邮件都会存到文件中,时间长了会将

           解决办法:定向到空即可

     >/dev/null 2>&1

    4.4 两种输出重定向

    >/dev/null  2>&1

           把正确的和错误的都放在 /dev/null(黑洞,一去不复返)

          

    >>/tmp/clsn.txt   2>&1

           把正确的和错误的都放在/tmp/clsn.txt

    4.5 书写定时任务要将内容输出重定向

    如果定时任务规则结尾不加>/dev/null 2>&1或者追加到文件中>>/tmp/clsn 2>&1很容易导致硬盘inode空间被占满,从而系统服务不正常。

    4.6 创建和删除大量小文件的方法

    4.6.1 删除小文件的方法

    1)大量的小文件

    [root@znix tmp]# ls |xargs rm

    2)删除文件所在的目录 (权限、所有者、属组)

    3)临时开启邮件服务 (只对/var/spool/postfix/maildrop 这个目录下的小文件)

    4.6.2 创建小文件的方法

    [root@znix tmp]# echo {1..400000}|xargs touch

    [root@znix tmp]# ll -hd  tmp/

    drwxr-xr-x 2 root root 8.4M Sep  8 10:59 tmp/

    4.7 管道与|xargs 区别:

    | 传递的是文本

    |xargs 后面的命令会任务传递过来的是文件

    4.8 定时任务的位置

    1.用户的定时任务中,默认存放在当前用户的家目录

    2.系统的定时任务存放在根下

    第5章 练习题--定时任务

    5.1 定时任务中的环境变量

    定时任务中,只能识别两个位置的变量,使用其他位置命令的时候可以使用绝对路径,也可以添加环境变量.

    /usr/bin

    /bin

    添加环境变量,直接写在定时任务的开始即可.可以将自己使用到的命令位置都添加上.

    export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

    5.2 每分钟显示当前的系统的ip地址还有系统日期date +%F 追加到文件中/tmp/ip.log

    5.2.1 先测试一下命令

    保证命令是执行没有错误的.

    [root@znix /]# date +%F

    2017-09-08

    [root@znix /]# /sbin/ifconfig  eth0 |awk -F "[ :]+" 'NR==2 {print $4}'

    10.0.0.201

    5.2.2 将命令放入脚本

    [root@znix ~]# cat /server/scripts/ip.sh

    date +%F

    /sbin/ifconfig  eth0 |awk -F "[ :]+" 'NR==2 {print $4}'

    5.2.3 书写定时任务

    [root@znix ~]# crontab -l |tail -2

    #print ip & date

    * * * * * /bin/sh /server/scripts/ip.sh  >/tmp/ip.sh 2>&1

    5.2.4 检查执行结果

    [root@znix tmp]# tail -f /tmp/ip.log

    2017-09-08

    10.0.0.201

    5.3 每隔 2 个小时将/etc/services 文件打包备份到/tmp下(最每次备份成不同的备份包)。

    5.3.1 测试命令是否正确

    [root@znix ~]# cd / && /bin/tar zcf /tmp/ser_`date +%F_%H`.tar.gz  etc/services

    [root@znix /]# ll tmp/ser_2017-09-08_1*

    -rw-r--r-- 1 root root 127314 Sep  8 16:15 tmp/ser_2017-09-08_16.tar.gz

    5.3.2 写入脚本中

    [root@znix ~]# cat /server/scripts/ser.sh

    cd / && /bin/tar zcf /tmp/ser_`date +%F_%H`.tar.gz  etc/services

    5.3.3 测试脚本

    [root@znix /]# sh /server/scripts/ser.sh

    [root@znix /]# ll tmp/ser_2017-09-08_1*

    -rw-r--r-- 1 root root 127314 Sep  8 16:17 tmp/ser_2017-09-08_16.tar.gz

    5.3.4 写入定时任务

    [root@znix ~]# crontab -l |tail -2

    ##dabao /etc/services

    00 */2 * * * /bin/sh /server/scripts/ser.sh >/dev/null 2>&1

    5.3.5 检查结果

    [root@znix /]# ll tmp/ser_2017-09-08_1*

    -rw-r--r-- 1 root root 127314 Sep  8 11:59 tmp/ser_2017-09-08_11.tar.gz

    -rw-r--r-- 1 root root 127314 Sep  8 12:11 tmp/ser_2017-09-08_12.tar.gz

    -rw-r--r-- 1 root root 127314 Sep  8 16:17 tmp/ser_2017-09-08_16.tar.gz

    5.4 每天晚上 12 点,打包站点目录/var/www/html  备份到/data 目录下(最好每次备份按时间生成不同的备份包)

    5.4.1 测试命令

    [root@znix /]# cd / && tar zcf /data/www_`date +%F`.tar.gz  var/www/html

    [root@znix /]#  ll /data/www*

    -rw-r--r-- 1 root root 117 Sep  8 16:21 /data/www_2017-09-08.tar.gz

    5.4.2 写入测试脚本

    [root@znix /]# cat /server/scripts/www.sh

    cd / && tar zcf /data/www_`date +%F`.tar.gz  var/www/html

    [root@znix /]#  ll /data/www*

    -rw-r--r-- 1 root root 117 Sep  8 16:21 /data/www_2017-09-08.tar.gz

    5.4.3 写入定时任务

    [root@znix /]# crontab -l |tail -2

    ####dabao tar

    00 00 * * * /bin/sh /server/scripts/www.sh >/dev/null 2>&1

    5.4.4 检查执行结果

    [root@znix /]# ll /data/www*

    -rw-r--r-- 1 root root 117 Sep  8 16:28 /data/www_2017-09-08.tar.gz

  • 相关阅读:
    日期格式化
    堆栈
    编写自己的C头文件
    线性表(gcc实现)
    排序的稳定性
    git创建和合并分支
    当单选input框改变时触发
    css样式定义
    div块显示在一行
    redis数据结构(一)
  • 原文地址:https://www.cnblogs.com/langqi250/p/9711660.html
Copyright © 2011-2022 走看看