首页 > 系统服务 > 详细

shell 简单脚本 2

时间:2015-12-11 15:08:34      阅读:317      评论:0      收藏:0      [点我收藏+]
#!/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

 

shell 简单脚本 2

原文:http://www.cnblogs.com/rxingyue/p/5038828.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!