tomcat定時啟動指令碼

liangxingwei發表於2017-12-14

1.先確定tomcat/bin目錄下的startup和shutdown指令碼能不能使用

[root@localhost bin]# ./startup.sh 
Cannot find /usr/local/tomcat8/bin/setclasspath.sh
This file is needed to run this program
複製程式碼

發現並不能直接使用

  • 這個問題的出現,表示環境變數沒配置好

    (連結)關於tomcat環境方面的解釋

  • 編輯環境變數 vi /etc/profile

  • 找到相關的配置

     \#export JAVA_HOME=/usr/java/jdk1.8
    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.92-1.b14.fc23.x86_64/jre
    export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CATALINA_BASE/lib 
    export CATALINA_BASE=/usr/local/tomcat8/
    export CATALINA_HOME=/usr/local/tomcat8/
    export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$CATALINA_BASE/bin
    複製程式碼
  • 其中的CATALINA_BASE和CATALINA_HOME並沒有指向正確,因為我tomcat8目錄下還有一層目錄,修改即可

  • 使修改生效: . /etc/profile(. /中間有一個空格)

  • 再次執行: ./startup.sh,成功

2.寫指令碼

  • 將下面的程式碼直接儲存到一個sh檔案

    #!/bin/sh

    export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.92-1.b14.fc23.x86_64/jre

    pid=ps -auxww|grep tomcat|grep -v grep|awk '{print $2}'

    echo $pid

    if [ -n "$pid" ];

    then

    {

    echo "==========tomcat is start============"

    '/usr/local/tomcat8/apache-tomcat-8.0.35/bin/shutdown.sh'

    sleep 5

    '/usr/local/tomcat8/apache-tomcat-8.0.35/bin/shutdown.sh'

    sleep 15

    pid=ps -auxww|grep tomcat|grep -v grep|awk '{print $2}'

    if [ -n "$pid" ];

    then

    {

    echo "======to kill the tomcat pid $pid========"

    kill $pid

    sleep 3

    }

    fi

    echo "==========start tomcat============"

    '/usr/local/tomcat8/apache-tomcat-8.0.35/bin/startup.sh'

    }

    else

    {

    echo "==========tomcat is stop=====start======="

    '/usr/local/tomcat8/apache-tomcat-8.0.35/bin/startup.sh'

    }

    fi

其中要注意Java_home和tomcat指令碼的路徑是不是正確

  • 測試下正不正確

    [root@localhost auto_exec]# ./test.sh bash: ./test.sh: 許可權不夠

  • 賦予許可權

    chmod 777 *.sh

  • 再次啟動

    [root@localhost auto_exec]# ./test.sh ----------tomcat is stop----start=======

    Using CATALINA_BASE: /usr/local/tomcat8/apache-tomcat-8.0.35 Using CATALINA_HOME: /usr/local/tomcat8/apache-tomcat-8.0.35 Using CATALINA_TMPDIR: /usr/local/tomcat8/apache-tomcat-8.0.35/temp Using JRE_HOME: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.92- 1.b14.fc23.x86_64/jre Using CLASSPATH: /usr/local/tomcat8/apache-tomcat-8.0.35/bin/bootstrap.jar:/usr/local/tomcat8/apache-tomcat-8.0.35/bin/tomcat-juli.jar Tomcat started.

  • 重啟

    [root@localhost auto_exec]# ./test.sh 10785 ==========tomcat is start============ Using CATALINA_BASE: /usr/local/tomcat8/apache-tomcat-8.0.35 Using CATALINA_HOME: /usr/local/tomcat8/apache-tomcat-8.0.35 Using CATALINA_TMPDIR: /usr/local/tomcat8/apache-tomcat-8.0.35/temp Using JRE_HOME: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.92-1.b14.fc23.x86_64/jre Using CLASSPATH: /usr/local/tomcat8/apache-tomcat-8.0.35/bin/bootstrap.jar:/usr/local/tomcat8/apache-tomcat-8.0.35/bin/tomcat-juli.jar 一月 12, 2017 9:51:24 上午 org.apache.catalina.startup.Catalina stopServer 嚴重: Could not contact localhost:8005. Tomcat may not be running. 一月 12, 2017 9:51:24 上午 org.apache.catalina.startup.Catalina stopServer 嚴重: Catalina.stop: java.net.ConnectException: 拒絕連線 at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at java.net.Socket.connect(Socket.java:538) at java.net.Socket.(Socket.java:434) at java.net.Socket.(Socket.java:211) at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:450) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:400) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:487)

    Using CATALINA_BASE: /usr/local/tomcat8/apache-tomcat-8.0.35 Using CATALINA_HOME: /usr/local/tomcat8/apache-tomcat-8.0.35 Using CATALINA_TMPDIR: /usr/local/tomcat8/apache-tomcat-8.0.35/temp Using JRE_HOME: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.92-1.b14.fc23.x86_64/jre Using CLASSPATH: /usr/local/tomcat8/apache-tomcat-8.0.35/bin/bootstrap.jar:/usr/local/tomcat8/apache-tomcat-8.0.35/bin/tomcat-juli.jar 一月 12, 2017 9:51:29 上午 org.apache.catalina.startup.Catalina stopServer 嚴重: Could not contact localhost:8005. Tomcat may not be running. 一月 12, 2017 9:51:30 上午 org.apache.catalina.startup.Catalina stopServer 嚴重: Catalina.stop: java.net.ConnectException: 拒絕連線 at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at java.net.Socket.connect(Socket.java:538) at java.net.Socket.(Socket.java:434) at java.net.Socket.(Socket.java:211) at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:450) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:400) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:487)

    ======to kill the tomcat pid 10785======== ==========start tomcat============ Using CATALINA_BASE: /usr/local/tomcat8/apache-tomcat-8.0.35 Using CATALINA_HOME: /usr/local/tomcat8/apache-tomcat-8.0.35 Using CATALINA_TMPDIR: /usr/local/tomcat8/apache-tomcat-8.0.35/temp Using JRE_HOME: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.92-1.b14.fc23.x86_64/jre Using CLASSPATH: /usr/local/tomcat8/apache-tomcat-8.0.35/bin/bootstrap.jar:/usr/local/tomcat8/apache-tomcat-8.0.35/bin/tomcat-juli.jar Tomcat started.

證明指令碼可以完美執行了

3.新增到定時任務

  • 使用crontab命令

    contab 具體用法

    其中crontab -e,新增新的定時任務

    輸入*/5 0 * * * /home/auto_exec/test.sh 表示每五分鐘執行一次

然後就大功告成

相關文章