新建脚本文件

touch retomcat.sh

vi retomcat.sh

  1 #!/bin/bash
  2 #tomcat部署路径
  3 tomcatPath1=/u01/kf/apache-tomcat-zhkf1
  4 tomcatPath2=/u01/kf/apache-tomcat-zhkf2
  5 tomcatPath3=/u01/kf/apache-tomcat-zhkf3
  6 
  7 echo "[info][$(date +'%F %H:%M:%S')]正在监控tomcat,路径:$tomcatPath1"
  8 # awk '{print $2}' 输出按空格分割的第二个字符
  9 pid=`ps -ef|grep tomcat | grep -w "${tomcatPath1}" | grep -v 'grep' | awk '{print $2}'`
 10 echo "[info]apache-tomcat-zhkf1进程号为${pid}"
 11 #如果进程号不为空
 12 if [ -n "${pid}" ]; then
 13  echo "[info][$(date +'%F %H:%M:%S')]正在运行的tomcat进程为:$pid"
 14  echo "[info][$(date +'%F %H:%M:%S')]tomcat已经启动,准备使用shutdown命令关闭..."
 15  $tomcatPath1"/bin/shutdown.sh"
 16  sleep 2
 17  pid=`ps -ef|grep tomcat | grep -w "${tomcatPath1}" | grep -v 'grep' | awk '{print $2}'`
 18  if [ -n "$pid" ]; then
 19   echo "[info][$(date +'%F %H:%M:%S')]使用shutdown命令关闭失败,准备kill进程..."
 20   kill -9 $pid
 21   echo "[info][$(date +'%F %H:%M:%S')]kill进程完毕!"
 22   sleep 1
 23  else
 24   echo "[info][$(date +'%F %H:%M:%S')]使用shutdown命令关闭成功!"
 25  fi
 26 #进程号为空
 27 else
 28  echo "[info][$(date +'%F %H:%M:%S')]tomcat未启动!"
 29 fi
 30 echo "[info][$(date +'%F %H:%M:%S')]准备启动tomcat..."
 31 $tomcatPath1"/bin/startup.sh"
 32 pid=`ps -ef|grep tomcat | grep -w "${tomcatPath1}" | grep -v 'grep' | awk '{print $2}'`
 33 if [ -n "${pid}" ]; then
 34  echo "[info][$(date +'%F %H:%M:%S')]$tomcatPath1重启成功"
 35 else
 36  echo "[info][$(date +'%F %H:%M:%S')]$tomcatPath1重启失败"
 37 
 38 fi
 39 
 40 
 41 echo "[info][$(date +'%F %H:%M:%S')]正在监控tomcat,路径:$tomcatPath2"
 42 # awk '{print $2}' 输出按空格分割的第二个字符
 43 pid=`ps -ef|grep tomcat | grep -w "${tomcatPath2}" | grep -v 'grep' | awk '{print $2}'`
 44 echo "[info]apache-tomcat-zhkf1进程号为${pid}"
 45 #如果进程号不为空
 46 if [ -n "${pid}" ]; then
 47  echo "[info][$(date +'%F %H:%M:%S')]正在运行的tomcat进程为:$pid"
 48  echo "[info][$(date +'%F %H:%M:%S')]tomcat已经启动,准备使用shutdown命令关闭..."
 49  $tomcatPath2"/bin/shutdown.sh"
 50  sleep 2
 51  pid=`ps -ef|grep tomcat | grep -w "${tomcatPath2}" | grep -v 'grep' | awk '{print $2}'`
 52  if [ -n "$pid" ]; then
 53   echo "[info][$(date +'%F %H:%M:%S')]使用shutdown命令关闭失败,准备kill进程..."
 54   kill -9 $pid
 55   echo "[info][$(date +'%F %H:%M:%S')]kill进程完毕!"
 56   sleep 1
 57  else
 58   echo "[info][$(date +'%F %H:%M:%S')]使用shutdown命令关闭成功!"
 59  fi
 60 #进程号为空
 61 else
 62  echo "[info][$(date +'%F %H:%M:%S')]tomcat未启动!"
 63 fi
 64 echo "[info][$(date +'%F %H:%M:%S')]准备启动tomcat..."
 65 $tomcatPath2"/bin/startup.sh"
 66 pid=`ps -ef|grep tomcat | grep -w "${tomcatPath2}" | grep -v 'grep' | awk '{print $2}'`
 67 if [ -n "${pid}" ]; then
 68  echo "[info][$(date +'%F %H:%M:%S')]$tomcatPath2重启成功"
 69 else
 70  echo "[info][$(date +'%F %H:%M:%S')]$tomcatPath2重启失败"
 71 
 72 fi
 73 
 74 echo "[info][$(date +'%F %H:%M:%S')]正在监控tomcat,路径:$tomcatPath3"
 75 # awk '{print $2}' 输出按空格分割的第二个字符
 76 pid=`ps -ef|grep tomcat | grep -w "${tomcatPath3}" | grep -v 'grep' | awk '{print $2}'`
 77 echo "[info]apache-tomcat-zhkf1进程号为${pid}"
 78 #如果进程号不为空
 79 if [ -n "${pid}" ]; then
 80  echo "[info][$(date +'%F %H:%M:%S')]正在运行的tomcat进程为:$pid"
 81  echo "[info][$(date +'%F %H:%M:%S')]tomcat已经启动,准备使用shutdown命令关闭..."
 82  $tomcatPath3"/bin/shutdown.sh"
 83  sleep 2
 84  pid=`ps -ef|grep tomcat | grep -w "${tomcatPath3}" | grep -v 'grep' | awk '{print $2}'`
 85  if [ -n "$pid" ]; then
 86   echo "[info][$(date +'%F %H:%M:%S')]使用shutdown命令关闭失败,准备kill进程..."
 87   kill -9 $pid
 88   echo "[info][$(date +'%F %H:%M:%S')]kill进程完毕!"
 89   sleep 1
 90  else
 91   echo "[info][$(date +'%F %H:%M:%S')]使用shutdown命令关闭成功!"
 92  fi
 93 #进程号为空
 94 else
 95  echo "[info][$(date +'%F %H:%M:%S')]tomcat未启动!"
 96 fi
 97 echo "[info][$(date +'%F %H:%M:%S')]准备启动tomcat..."
 98 $tomcatPath3"/bin/startup.sh"
 99 pid=`ps -ef|grep tomcat | grep -w "${tomcatPath3}" | grep -v 'grep' | awk '{print $2}'`
100 if [ -n "${pid}" ]; then
101  echo "[info][$(date +'%F %H:%M:%S')]$tomcatPath3重启成功"
102 else
103  echo "[info][$(date +'%F %H:%M:%S')]$tomcatPath3重启失败"
104 
105 fi

对可执行文件授权
chmod +x retomcat.sh
执行脚本命令
./retomcat.sh > /home/cspgs/retomcat.logs 2>&1

查看后台打印日志

tail -f retomcat.logs

另:

shell中每个进程都和三个系统文件相关联

  • 标准输入stdin
  • 标准输出stdout
  • 标准错误stderr

三个系统文件的文件描述符分别为012
所以这里2>&1的意思就是将标准错误也输出到标准输出当中。

posted on 2020-10-30 15:14  唐小夏  阅读(433)  评论(0编辑  收藏  举报