通过pid杀死进程,然后重启服务(tomcat重启)

来源:互联网 发布:c语言输入学生成绩 编辑:程序博客网 时间:2024/05/22 08:22

问题引出

tomcat有自己的脚本(shutdown.sh),来关闭服务,但是当tomcat容器中的项目开启了其他的端口做相关逻辑操作时,tomcat的关闭服务脚本就无能为力了,它只能关闭自己的端口

解决方案


  • 直接杀死进程

直接杀死进程会将其捆绑的所有服务停止掉,不管你这个进程上绑定了多少个端口

代码实践

  • 我们投产的服务器比较变态,不支持中文编码,所以只好双管齐下,中英结合了
  • 脚本中注释掉的代码部分为另一种从控制台接收参数的方式,不过考虑到时候不能每次都从控制台输入参数来执行重启(毕竟重启的可能是运维人员,为了避免不必要的麻烦,还是不要让他们手动输入了),所以改成了直接通过配置修改TOMCAT_HOME 的方式
#!/bin/bash## author:郑明亮## time:2017年8月28日15:19:57## 执行脚本前,修改这个变量 (modify this variable before execute the shell script)export TOMCAT_HOME=/opt/cim-tomcat-8080#echo "-------Input process name first"## 从控制台读取要杀的进程名(read the value from the console)#read input1## 获取pid (get the pid)#PID=$(ps x|grep $input1|grep -v grep|awk '{printf $1}')PID=$(ps x|grep $TOMCAT_HOME|grep -v grep|awk '{printf $1}')  if [ $? -eq 0 ]; then    echo "---------process id(进程id):$PID"    echo "-------begin to kill the pid(开始杀进程)"    kill -9 $PID      if [ $? -eq 0 ];then#        echo "----------kill $input1 success(成功杀死进程)"  echo "----------kill $TOMCAT_HOME success(成功杀死进程)"     else       echo "----------kill $input1 fail(杀死进程失败,可能该进程不存在)"    fi  else## 实际测试:进程不存在时不会走这个else(in fact,this situation will not happen)    echo "---------process $input1 not exit(该进程不存在)"    exit  fi## 睡眠3秒,确保进程已停止(sleep 3s make sure the process has killed)echo "-------wait for closing the tomcat(等待tomcat被关闭)"sleep 3secho "-------begain to start the tomcat service (开始启动tomcat服务)"$TOMCAT_HOME/bin/startup.sh && tail -f $TOMCAT_HOME/logs/catalina.out

效果图

出处

本文首次发布于个人博客:吾勇士的博客,转载请标明出处,更多文章可点击右上角“My pesional blog”

原创粉丝点击