如何监视tomcat应用程序,如果未运行则重新启动?

如何监视tomcat应用程序,如果未运行则重新启动?

我有一个在 Tomcat 上运行的 Web 应用程序,并希望有一种自动监控应用程序的方法,以确保它正在运行,如果没有,则启动/重新启动 tomcat(并发送电子邮件警报)。我想做的不仅仅是检查 Tomcat 是否正在运行,并确保可以加载应用程序中的默认页面。

除了编写 shell 脚本并将其作为 cron 作业运行之外,是否有任何工具可以最好地完成此操作?

答案1

请查看监控公用事业

这是一个守护进程和进程监视工具,可以提供您可能需要的警报和定义的操作。

一个简单的例子:

check process tomcat with pidfile "/var/run/tomcat/tomcat.pid"
  start program = "/usr/local/tomcat/bin/startup.sh" as uid tomcat gid tomcat
  stop program = "/usr/local/tomcat/bin/shutdown.sh" as uid tomcat gid tomcat
  if failed port 8080 then alert
  if failed port 8080 for 5 cycles then restart

答案2

这可能会对某些人有帮助!!

如果不想使用任何监控工具,则使用 mailutils 包在 Ubuntu 服务器中设置电子邮件配置。

https://rianjs.net/2013/08/send-email-from-linux-server-using-gmail-and-ubuntu-two-factor-authentication

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-14-04

要监控 Tomcat 状态,您可以使用以下脚本并根据需要设置每分钟/小时/天运行的 cron 作业。

#!/bin/bash

TOMCAT_HOME=/opt/tomcat
PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
EMAIL_BODY="Hi Admin,\n\n$PUBLIC_IP Tomcat is down at $(date -d "+330 minutes" +"%Y-%m-%d %T") IST, Please take necessary action.\n\n\nDo not reply to this email as it is auto generated by Ubuntu system\n"

tomcat_pid() {
  echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ] 
  then
    echo "Tomcat is already running (pid: $pid)"
  else
    # Start tomcat
    echo "Starting tomcat"
    /bin/sh $TOMCAT_HOME/bin/startup.sh
  fi


  return 0
}

pid=$(tomcat_pid)

 if [ -n "$pid" ]
  then
    echo "Tomcat is running with pid: $pid"
    #stop
  else
    echo "Tomcat is not running"
    # send an email alert then start
    echo -e $EMAIL_BODY | mail -s "$PUBLIC_IP Tomcat is down" [email protected]
    echo "Mail sent"
    #remove cache and release memory occupied by heavy processes
    start
  fi
exit 0

相关内容