zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    投资银行的IT部门——不同之处与常见误解
    C++ error C2440: “类型转换” : 无法从“std::vector::iterator”转换为“
    查看端口占用
    Sc config http start= disabled
    DDL、DML和DCL的区别与理解
    不同网段,在路由器上如何设置网关
    服务器调优
    查看SQL Server版本信息
    基于32位Windows2003的数据库服务器优化,启用AWE,优化SQL Server
    在WIN7操作系统下,如何显示文件夹里文件的数目
  • 原文地址:https://www.cnblogs.com/rxingyue/p/5038828.html
Copyright © 2011-2022 走看看