network 发表于 2018-8-21 13:39:16

Bash shell脚本练习(五)

  1.监控apache服务状态
  


[*]#!/bin/bash
[*]
[*]. /etc/init.d/functions
[*]HTTPPRONUM=`ps -ef | grep http | grep -v grep | wc -l`
[*]
[*]if [ $HTTPPRONUM -lt 1 ]; then
[*]      action "httpd is not runing" /bin/false
[*]      action "httpd is not runing" /bin/false > /tmp/httpd.log
[*]      apachectl restart > /dev/null 2>&1
[*]         action "httpd is restart" /bin/true
[*]      mail -s &quot;`uname -n`'s httpd restarted at `(date)`&quot; root@example.com < /tmp/httpd.log
[*]      exit 1
[*]else
[*]      action &quot;httpd is runing&quot; /bin/true
[*]      exit 0
[*]fi
  

2.每10分钟检测一次/usr目录是否超过5G  


[*]#!/bin/bash
[*]while true
[*]do
[*]      sleep 600
[*]      n=$(du -s /usr | cut -f 1)
[*]
[*]      if [ $n -gt 5242880 ]; then
[*]                echo &quot;/usr目录大于5G&quot; | mail -s &quot;warning&quot; root@example.com
[*]      fi
[*]done
  

3.处理文本,要求合并文件后的输出以下结果:
400 ashok sharma $1,250
100 jason smith$5,000
200 john doe$500
300 sanjay gupta$3,000
# cat employee.txt
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
# cat bonus.txt
100 $5,000
200 $500
300 $3,000
400 $1,250
# paste employee.txt bonus.txt | awk '{print $1,$2,$3,$5}'|tr '' '' | sort -k 2
400 ashok sharma $1,250
100 jason smith $5,000
200 john doe $500
300 sanjay gupta $3,000

页: [1]
查看完整版本: Bash shell脚本练习(五)