Maison  >  Questions et réponses  >  le corps du texte

ubuntu server 下tomcat 如何实现在设定的时间内重启

我想让ubuntu下的tomcat7 实现8个小时就自动重启一次。要怎么弄?
求各路大神献计献策。谢谢!

PHP中文网PHP中文网2739 Il y a quelques jours507

répondre à tous(3)je répondrai

  • PHP中文网

    PHP中文网2017-04-21 11:17:16

    Laissez-moi chercher ceci pour vous
    http://stackoverflow.com/questions/20454435/how-to-restart-a-process-every-4-hours-using-crontab
    Écrivez ceci toutes les huit heures.
    * */8 * * * user-name command to be executed

    D'ailleurs, je recommande un script fiable pour gérer Tomcat

    #!/bin/bash
    #
    # tomcat     This shell script takes care of starting and stopping Tomcat
    #
    # chkconfig: - 80 20
    #
    ### BEGIN INIT INFO
    # Provides: tomcat
    # Required-Start: $network $syslog
    # Required-Stop: $network $syslog
    # Default-Start:
    # Default-Stop:
    # Short-Description: start and stop tomcat
    ### END INIT INFO
    export JAVA_HOME=/usr/java/jdk1.6.0_43 
    TOMCAT_HOME=/usr/local/apache-tomcat-6.0.37
    SHUTDOWN_WAIT=45
    
    tomcat_pid() {
        echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print  }'`
    }
    
    start() {
        pid=$(tomcat_pid)
        if [ -n "$pid" ]
        then
            echo "Tomcat is already running (pid: $pid)"
        else
            # Start tomcat
            echo "Starting tomcat"
            $TOMCAT_HOME/bin/startup.sh
        fi
        return 0
    }
    
    stop() {
        pid=$(tomcat_pid)
        if [ -n "$pid" ]
        then
            echo "Stoping Tomcat"
            $TOMCAT_HOME/bin/shutdown.sh
    
        let kwait=$SHUTDOWN_WAIT
        count=0
        count_by=5
        until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
        do
            echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
            sleep $count_by
            let count=$count+$count_by;
        done
    
        if [ $count -gt $kwait ]; then
            echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
            kill -9 $pid
        fi
        else
            echo "Tomcat is not running"
        fi
    
        return 0
    }
    
    case  in
        start)
            start
            ;;
        stop)
            stop
            ;;
        restart)
            stop
            start
            ;;
        status)
           pid=$(tomcat_pid)
            if [ -n "$pid" ]
            then
               echo "Tomcat is running with pid: $pid"
            else
               echo "Tomcat is not running"
            fi
            ;;
    esac
    
    exit 0
    

    répondre
    0
  • 迷茫

    迷茫2017-04-21 11:17:16

    Utilisez crontab pour résoudre les tâches planifiées.

    # 设置定时任务
    root> crontab -e
    # 8点,每天,每月执行脚本
    root> 0 8 * * *  /full/path/to/tomcat/restart/script
    # 查看定时任务列表
    root> crontab -l
    

    http://stackoverflow.com/questions/3474280/how-to-set-up-a-cron-job-to-run-an-executable-every-hour?answertab=oldest#tab-top

    répondre
    0
  • PHPz

    PHPz2017-04-21 11:17:16

    #!/bin/sh

    . /etc/profil

    pid=ps aux | grep tomcat7_portal | grep -v grep | grep -v retomcat7_portal | awk '{print }'
    echo $pid

    if [ -n "$pid" ]
    then
    {
    echo ===========shutdown============== ==
    /opt/tomcat7_portal/bin/shutdown.sh
    sleep 1
    pid=ps aux | grep tomcat7_portal | grep -v grep | grep -v retomcat7_portal | awk '{print }'
    if [ -n "$pid" ]
    then

    {
      sleep 1 
      echo ========kill tomcat7_portal==============  
      kill -9 $pid
    }

    fi
    sommeil 1
    echo ===========startup.sh==============
    /opt/tomcat7_portal/bin /startup.sh
    }
    else
    echo ===========startup.sh==============
    /opt/ tomcat7_portal/bin/startup.sh

    fi

    répondre
    0
  • Annulerrépondre