2017/11/14

2017/11/14 周二;

天气: 小雨
1、需求:

  1. 找到/123目录下所有后缀名为.txt的文件
  2. 批量修改.txt为.txt.bak
  3. 把所有.bak文件打包压缩为123.tar.gz
  4. 批量还原文件的名字,即把增加的.bak再删除
[root@Dasoncheng sbin]# mkdir /123  ##创建一个根目录文件夹,用绝对路径
[root@Dasoncheng sbin]# cat f.sh 
#!/bin/bash
find /123 -type f -name "*.txt" >/tmp/txt.list
for i in `cat /tmp/txt.list`;
do
  mv $i $i.bak
done
d=`date +%Y%m%d%H%M%S`
mkdir /tmp/123_$d
for i in `cat /tmp/txt.list`;
do
  cp $i.bak /tmp/123_$d
done
cd /tmp
tar -czf 123.tar.gz 123_$d
for i in `cat /tmp/txt.list`;
do 
  mv $i.bak $i
done

2、需求:
写一个脚本,判断本机的80端口(假如服务为httpd)是否开启着,如果开启着什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知你自己。脚本写好后,可以每一分钟执行一次,也可以写一个死循环的脚本,30s检测一次。

[root@Dasoncheng sbin]# cat e.sh 
#!/bin/bash
mail=gecz0000@163.com
while :;
do
  if netstat -lntp |grep ':80' |grep -q 'LISTEN' ;
  then
    sleep 30
    continue
  else
    /usr/sbin/apachectl restart >/dev/null 2>/dev/null
    python mail.py $mail "Check_80" "The 80 port is down."
    n=`ps aux |grep httpd |grep -cv grep`
    if [ $n -eq 0 ];
    then
      /usr/sbin/apachectl start 2> /tmp/apache_start.err
      if [ -s /tmp/apache_start.err ] ;
      then
        python mail.py $mail "apache_start_err" "cat /tmp/apache_start.err" 
      fi
    fi
    sleep 30
    continue
  fi
done
##测试:
[root@Dasoncheng sbin]# systemctl stop httpd
[root@Dasoncheng sbin]# sh -x e.sh 
+ mail=gecz0000@163.com
+ :
+ netstat -lntp
+ grep -q LISTEN
+ grep :80
+ /usr/sbin/apachectl restart
+ python mail.py gecz0000@163.com Check_80 'The 80 port is down.'
++ ps aux
++ grep httpd
++ grep -cv grep
+ n=6
+ '[' 6 -eq 0 ']'
+ :
+ netstat -lntp
+ grep -q LISTEN
+ grep :80
+ sleep 30
+ continue
+ :
+ netstat -lntp
+ grep -q LISTEN
+ grep :80
+ sleep 30
^C
参考答案:

1、

#!/bin/bash
##查找txt文件
find /123 -type f -name “*.txt” > /tmp/txt.list
##批量修改文件名
for f in `cat /tmp/txt.list`
do
mv $f $f.bak
done
##创建一个目录,为了避免目录已经存在,所以要加一个复杂的后缀名
d=`date +%y%m%d%H%M%S`
mkdir /tmp/123_$d
##把.bak文件拷贝到/tmp/123_$d
for f in `cat /tmp/txt.list`
do
cp $f.bak /tmp/123_$d
done
##打包压缩
cd /tmp/
tar czf 123.tar.gz 123_$d/
##还原
for f in `cat /tmp/txt.list`
do
mv $f.bak $f
done

2、

#! /bin/bash
mail=123@123.com
if netstat -lnp |grep ‘:80’ |grep -q ‘LISTEN’; then
exit
else
/usr/local/apache2/bin/apachectl restart >/dev/null 2> /dev/null
python mail.py $mail “check_80” “The 80 port is down.”
n=`ps aux |grep httpd|grep -cv grep`
if [ $n -eq 0 ]; then
/usr/local/apache2/bin/apachectl start 2>/tmp/apache_start.err
fi
if [ -s /tmp/apache_start.err ]; then
python mail.py  $mail  ‘apache_start_error’   `cat /tmp/apache_start.err`
fi
fi

转载于:https://my.oschina.net/u/3651233/blog/1573377

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值