Shell脚本不工作的过程只是杀死了两个进程,除了三个进程

时间:2016-03-22 03:08:39

标签: bash shell unix crontab

以下脚本未通过crontab运行。请告诉我解决此问题需要哪个步骤。当我手动触发时,此脚本运行正常。

Crontab: -

### Repack Restart
33 01 * * 1,2,3,4,5  /home/repackpr/scripts/repack_restart_script.sh > /tmp/repack_log16234.log 2>&1

repack_restart_script.sh:

 #!/usr/bin/bash
    cd /home/repackpr
    today_date=`date '+%Y%m%d'`
    DATE_STAMP=`TZ=CST+24 date +%Y%m%d`

tomcat_pid=$(pgrep -U $USER java)
rmireg_pid=$(pgrep -U $USER rmiregistry)
nagios_pid=$(pgrep -U $USER nrpe)

export tomcat_pid
export rmireg_pid
export nagios_pid

    #Removing old logs

    rm /home/repackpr/scripts/Repack_$DATE_STAMP.log
    rm /home/repackpr/scripts/Repack_feed_mgt_$DATE_STAMP.log
    rm /home/repackpr/scripts/Repack_Stop_$DATE_STAMP.log

    # Stopping Services
    /home/repackpr/server stop
    kill -9 `ps -fu repackpr -o pid,comm | egrep 'java|repackpr' | awk '{print $1}'`
    /home/repackpr/server status > /home/repackpr/scripts/Repack_Stop_$today_date.log

    #Starting Services
    export CATALINA_OPTS="-Dcom.sun.management.jmxremote.port=8071 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
    /home/repackpr/server start
    /home/repackpr/server status > /home/repackpr/scripts/Repack_$today_date.log

    VALUE=`grep up /home/repackpr/scripts/Repack_$today_date.log|wc -l`
    if [ $VALUE -eq 3 ]
    then
        /apps/repack/jetty/start.sh > /home/repackpr/scripts/Repack_feed_mgt_$today_date.log &  #####Starting Repack Feed Management######
        echo "Repack services has been started successfully"|mailx -s "Repack Service Restart--SUCCESS" rates-it_pune@xyz.com     else
        echo "Please perform the Repack service restart check" |mailx -s "Repack Service Restart--FAIL" rates-it_pune@list.xyz.com
    fi

服务器脚本: - 此脚本从repack_restart_script.sh调用 - >即/ home / repackpr / server stop

#!/bin/bash
# 

wait-n-kill () {
    echo Waiting up to a minute for a graceful shutdown
    for(( i=0; $i<60; i=$(($i+1)) )); do
       sleep 1
       kill -0 $1 2>/dev/null || break
       echo -n .
    done
    echo
    kill -0 $1 2>/dev/null && echo Still alive ... Killing it with dash nine && kill -9 $1
}

tomcat_pid=$(pgrep -U $USER java)
rmireg_pid=$(pgrep -U $USER rmiregistry)
nagios_pid=$(pgrep -U $USER nrpe)

case "$1" in
      "start")
          if [ -z $tomcat_pid ] ; then
             suffix=$(date +%Y%m%d-%H%M)
             echo Repack is not running. Creating rpdb.log.$suffix backup...
             cp /apps/repack/tomcat/webapps/rpdb/WEB-INF/fwk/log/rpdb.log /apps/repack/tomcat/webapps/rpdb/WEB-INF/fwk/log/rpdb.log.$suffix
             gzip /apps/repack/tomcat/webapps/rpdb/WEB-INF/fwk/log/rpdb.log.$suffix
             echo Starting Repack... This should start rmiregistry as well.
             $TOMCAT_EXE/startup.sh
          else
             echo Repack is already running
          fi
          if [ -f ~/monitoring/go.sh  ] ; then
             if [ -z $nagios_pid ] ; then
                echo Starting Nagios agent
                cd ~/monitoring && ./go.sh
             fi
          fi ;;
      "stop")
          if [ ! -z $tomcat_pid ] ; then
             echo Stopping Repack
             $TOMCAT_EXE/shutdown.sh
             wait-n-kill $tomcat_pid
          fi
          [ ! -z $rmireg_pid ] && echo Killing rmiregistry && kill $rmireg_pid && wait-n-kill $rmireg_pid
          [ ! -z $nagios_pid ] && echo Killing Nagios && kill $nagios_pid && wait-n-kill $nagios_pid
esac

tomcat_pid=$(pgrep -U $USER java)
rmireg_pid=$(pgrep -U $USER rmiregistry)
nagios_pid=$(pgrep -U $USER nrpe)

[ ! -z "$tomcat_pid" ] &&                               echo "Repack      is up ($tomcat_pid)" || echo "Repack      is down"
[ ! -z "$rmireg_pid" ] &&                               echo "rmiregistry is up ($rmireg_pid)" || echo "rmiregistry is down"
[ $REPACK_ENV = "PROD" ] && ( [ ! -z "$nagios_pid" ] && echo "Nagios      is up ($nagios_pid)" || echo "Nagios      is down" )

/tmp/repack_log16234.log

 pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        /home/repackpr/scripts/Repack_20160322.log: No such file or directory
        /home/repackpr/scripts/Repack_feed_mgt_20160322.log: No such file or directory
        pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        Repack      is down
        rmiregistry is down
        /home/repackpr/server: line 54: [: =: unary operator expected
        pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        /home/repackpr/server: line 54: [: =: unary operator expected
        pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        Repack is not running. Creating rpdb.log.20160323-0133 backup...
        Starting Repack... This should start rmiregistry as well.
        /home/repackpr/server: line 28: /startup.sh: No such file or directory
        Starting Nagios agent
        Nagios agent has been started
        pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        Repack      is down
        rmiregistry is down
        /home/repackpr/server: line 54: [: =: unary operator expected
        pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        pgrep: invalid user name -- java
        pgrep: invalid user name -- rmiregistry
        pgrep: invalid user name -- nrpe
        /home/repackpr/server: line 54: [: =: unary operator expected

2 个答案:

答案 0 :(得分:0)

编辑: cron在服务器定义的时区上运行。您需要检查您的服务器时区,然后设置自己的cron。假设您的服务器时区是GMT:

double a; double b; while (fin_average >> a && fin_rainFall >> b){ fout <<"\t" << (a-b) << endl; }

cron格式如下:

33 01 * * 1-5 command-to-be-executed

检查更多cron格式示例here

请提供有关cron运行脚本的频率的更多信息,然后我们可以帮助确定输出应该是什么样的。

答案 1 :(得分:0)

与命令行中运行的作业不同的cron作业的问题源于这样一个事实:环境以不同的方式设置。

错误消息表明,pgrep在某处使用不正确的用户名调用,可能是因为缺少环境变量。由于您要发布的代码中没有~repackpr/server,我想这是在您的cron作业调用的某个脚本中调用的,该脚本必须是/apps/repack/jetty/start.shpgrep。尝试找到<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#frmCalc input:button").on("click", function() { var par = $(this).val(); $("#result").val($("#result").val() + par); }); }); </script> <form action="calculator.php" id="frmCalc" method="POST"> <input type="text" name="result" id="result"> <br> <input type="button" name="one" value="1"> <input type="button" name="two" value="2"> <input type="button" name="three" value="3"> <input type="button" name="four" value="4"> <input type="button" name="five" value="5"> <input type="button" name="six" value="6"> <input type="button" name="seven" value="7"> <input type="button" name="eight" value="8"> <input type="button" name="nine" value="9"> <input type="button" name="0" value="0"> <input type="button" name="+" value="+"> <input type="button" name="-" value="-"> <input type="button" name="*" value="*"> </form> 被调用的地方,并从那里向后工作。