shell 简单脚本 2

#!/bin/bash

source /etc/profile

APPLICATIONS_HOME="/cpic/cpicapp/cpic_analy/jars"
APPLICATION_NAME="CountFoodScore.jar"
SNAME=`basename $APPLICATION_NAME .jar`
LOG_DIR=`dirname $APPLICATIONS_HOME`/logs/$SNAME

STR_LOG=$(date +'%Y-%m-%d_%H-%M')
STR_DAY=$(date +'%Y-%m-%d')
STR_BEGIN=$(date +'%Y-%m-%d %H:%M:%S')

#This args is very important.It's when kill the task, it's type is seconds. default is 216000 seconds(6 hours).After 6 hours kill the task.if it exists.
KILL_THRESHOLD=3600

# This is the shell script's name.
NAME=`basename $0`

# if the LOG_DIR dictory is not exists, then make it.
if [ ! -d ${LOG_DIR} ]; then
    mkdir -p ${LOG_DIR}
fi

# if  this shell script is running, then exit.
if [ $(ps -ef|awk  -v n=${NAME} '$2!=p && $NF~n'|wc -c) -gt 300 ]; then

    # if the application is running,then get the pid.
    CHID=`ps -ef | grep -v grep | grep $APPLICATION_NAME |  awk '{print $2}'`
    echo "This chid is : " $CHID >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1

    PID=`ps -ef | grep -v grep | grep $CHID | awk '{print $3}'`
    echo "This pid is : " $PID >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1

    START_TIME=`ps -eo pid,lstart  | grep $PID`

    TEMP_TIME=${START_TIME#*' '}
    echo "The temp time is:"$TEMP_TIME
    
    FORMAT_START_TIME=`date -d "$TEMP_TIME" +%s`
    echo "Format start time is : " $FORMAT_START_TIME  >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1

    NOW_TIME=`date`
    FORMAT_NOW_TIME=`date -d "$NOW_TIME" +%s`
    echo " now time is : " $FORMAT_NOW_TIME >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1

    # If (this time - start time) is equals 21600, kill the task and restart.
    if [ `expr $FORMAT_NOW_TIME - $FORMAT_START_TIME` -gt $KILL_THRESHOLD  ]; then
        echo "This time is :"`expr $FORMAT_NOW_TIME - $FORMAT_START_TIME`   >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1
        echo $(date +'%Y-%m-%d %H:%M:%S') "This $PID task is running long time, killing it!" >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1
        kill -9 $CHID
        echo $STR_BEGIN "Restart it ......." >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1
        cd ${APPLICATIONS_HOME} && $JAVA_HOME/bin/java -jar ${APPLICATIONS_HOME}/${APPLICATION_NAME} >> "${LOG_DIR}/run_${SNAME}_${STR_LOG}.log" 2>&1
        exit
    else
        echo ${STR_BEGIN}" ${SNAME} is already running,exit!" >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1
        exit
    fi
else 
    echo ${STR_BEGIN}" ${SNAME} begin runing" >> $LOG_DIR/run_${STR_DAY}_sys.log  2>&1
    cd ${APPLICATIONS_HOME} && $JAVA_HOME/bin/java -jar ${APPLICATIONS_HOME}/${APPLICATION_NAME} >> "${LOG_DIR}/run_${SNAME}_${STR_LOG}.log" 2>&1
    STR_END=$(date +'%Y-%m-%d %H:%M:%S')
    echo ${STR_END}" ${SNAME} end runing" >> $LOG_DIR/run_${STR_DAY}_sys.log 2>&1
fi
exit

 

posted @ 2015-12-11 14:01  R星月  阅读(242)  评论(0编辑  收藏  举报
作者:lishaoying 出处:http://www.cnblogs.com/rxingyue 说明:本文是自己学习编程的一个历程,版权归作者和博客园共有,欢迎转载,请标明原文连接,如有问题联系我 Email:983068303@qq.com,非常感谢。

作者:lishaoying
出处:http://www.cnblogs.com/rxingyue
说明:本文是自己学习编程的一个历程,版权归作者和博客园共有,欢迎转载,请标明原文连接,如有问题联系我,非常感谢。