day 13 例题6-10
例题6:
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 6.sh
#检测80端口是否开启,没有就重启端口
#!/bin/bash
m=467926745@qq.com
n=`netstat -lntp |grep ':80 ' |wc -l`
while :
do
if [$n -eq 0 ]
then
/usr/local/apache2/bin/apachectl -k restart
python mail.py $m "80guanbi ""chongqi fuwu"
pn=`pgrep -l httpd|wc -l`
if [ $pn -eq 0 ]
then
python mail.py $m "chongqishibai" "head -1 /tmp/apache.err"
fi
fi
sleep 30
done
~
例题7:
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 7.sh #备份数据库,首先在本地服务器上保存一份数据,然后再远程拷贝一份,本地保存一周的数据,远程保存一个月 #!/bin/bash d1=`date +%w` d2=`date +%d` local_bakdir=/bak/mysql remote_bakdir=192.168.123.30::backup exec 1> /tmp/mysqlbak.log 2>/tmp/mysqlbak.err echo "mysql bakcup begin at `date`" mysqldump -uroot -pxxxx discz > $local_bakdir/discuz.sql.$d1 rsync -az $local_bakdir/discuz.sql.$d1 $remote_bakdir/discuz.sql.$d2 echo "mysql backup end at `date`" ~
例题8:
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 8.sh #监控访问日志的状态码,一旦发生502,则自动重启一下php-fpm #!/bin/bash log=/data/log/access.log while : do 502_n=`tail -n 300 $log |grep -c ' 502 '` if [ -z "$502_n" ] then exit fi if [ $502_n -gt 100 ] then /etc/init.d/php-fpm restart >/dev/null 2>/tmp/php-fpm.err fpm_p_n=`pgrep -l php-fpm|wc -l` if [ $fpm_p_n -eq 0 ] then python mail.py 111@qq.com "php-fpm重启失败" "`head -1 /tmp/php-fpm.err`" exit fi fi sleep 10 done ~
例题9:
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 9.sh #把一个文本文档的前5行中包含字母的行删除掉,同时把6到10行中的全部字母删除掉 #!/bin/bash sed -n '1,5'p 1.txt |sed '/[a-zA-Z]/d' sed '1,5d' 1.txt |sed '1,5s/[a-zA-Z]//g' ~
例题10:
[root@iZwz96qzfgxh9l2rk7esxnZ xiti]# vim 10.sh #打印字母数小于6的单词 #!/bin/bash c="Bash also interprets a number of multi-character options." n=`echo $c|awk -F '[ +-.]' '{print NF}'` for ((i=1;i<$n;i++)) do l=`echo $c|awk -F '[ +-.]' -v j=$i '{print $j}'|wc -L` if [ $l -lt 6 ] then echo $c|awk -F '[ +-.]' -v j=$i '{print $j}' fi done ~