当前位置:首页 >> 脚本专栏

SHELL脚本监控JAVA进程的代码

脚本内容:java_check.sh

#! /bin/bash  
#  
if [ -z `pidof java` ]; then  
  echo -e `date +%Y-%m-%d` `date +%R`. “33[31m33[1m(JAVA)Process does not exist! 33[0m”  /data/logs/java_check.log  
  /usr/local/tomcat/bin/startup.sh >/dev/null 2>&1  
  sleep 30  
  netstat -tnlp  /data/logs/java_check.log  
  echo -e “r”  /data/logs/java_check.log  
fi

计划任务,十五分钟执行一次
复制代码 代码如下:
*/15 * * * * root /usr/local/bin/java_check.sh


一个监控java程序运行的shell

#!/bin/sh
export J_LIB=/data/sms/lib;
export CLASSPATH=.:$J_LIB/jtds-1.2.jar:$J_LIB/log4j-1.2.12.jar:$J_LIB/postgresql-8.1-404.jdbc3.jar;
export JAVA_HOME=/usr/java/jdk1.5.0_05
export JRE=/usr/local/jdk/jre
export SHELL=/bin/bash
export HOME=/data/sms
#java check status
if /bin/ps ax | /bin/grep -v grep | /bin/grep "java XinTai"; then
 cd;
else
  cd /data/sms/xintai;
  /usr/java/jdk1.5.0_05/bin/java XinTai >/dev/null &
fi

定时执行可以保证 java XinTai可以一直在运行