shell 简单脚本 2

计分应用Shell脚本
#!/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

 

转载于:https://www.cnblogs.com/rxingyue/p/5038828.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值