一、將下面文件考入到linux 下 命名爲tomcat.shjava
#!/bin/bash tomcat_home=/usr/tomcat/apache-tomcat-8.5.27/ SHUTDOWN=$tomcat_home/bin/shutdown.sh STARTTOMCAT=$tomcat_home/bin/startup.sh case $1 in start) echo "---------------------------------------------------" echo "------------啓動$tomcat_home ---------------" echo "----------------------------------------------------" $STARTTOMCAT if [ $2 == "-l" ]; then echo "---------------------------------------------------" echo "---------------啓動日誌 ---------------------------" echo "----------------------------------------------------" tail -f $tomcat_home/logs/catalina.out fi ;; stop) echo "---------------------------------------------------" echo "----------------關閉$tomcat_home--------------" echo "---------------------------------------------------" $SHUTDOWN pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'` kill -9 $pidlist #刪除日誌文件,若是你不先刪除能夠不要下面一行 rm $tomcat_home/logs/* -rf #刪除tomcat的臨時目錄 rm $tomcat_home/work/* -rf ;; restart) echo "---------------------------------------------------" echo "----------------關閉$tomcat_home--------------" echo "---------------------------------------------------" $SHUTDOWN pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'` kill -9 $pidlist #刪除日誌文件,若是你不先刪除能夠不要下面一行 rm $tomcat_home/logs/* -rf #刪除tomcat的臨時目錄 rm $tomcat_home/work/* -rf sleep 5 echo "---------------------------------------------------" echo "------------啓動$tomcat_home ---------------" echo "----------------------------------------------------" $STARTTOMCAT #看啓動日誌 echo "---------------------------------------------------" echo "------------------啓動日誌 -------------------------" echo "----------------------------------------------------" tail -f $tomcat_home/logs/catalina.out ;; logs) tail -f $tomcat_home/logs/catalina.out ;; tomcat) echo "查看是否有正在運行的java程序,請關注tomcat運行狀況" ps -ef|grep tomcat ;; *) echo "啓動start | 啓動並日志start -l | 重啓restart | 關閉stop |日誌 logs | 查看tomcat" ;; esac
二、給tomcat.sh 受運行權限 linux
#如文件在 /usr/local/tomcat.sh chmod u+x /usr/local/tomcat.sh
三、給文件 加 符號連接apache
cd /usr/bin ln -s -f /usr/local/tomcat.sh tom
四、測試運行tomcat
#查看tomcat運行狀況 tom tomcat #啓動tomcat 並查看日誌 tom start -l #啓動tomcat tom start #重啓tomcat tom restart #中止tomcat tom stop #查看實時日誌 tom logs
五、若是要設置tomcat開機啓動的話,須要將tomcat.sh 複製到/etc/init.d/tomcat ,而後編輯tomcat文件在第二行加上啓動配置 # chkconfig 345 99 10 ( 345 是默認運行的級別,這裏是在3,4,5級別啓動,99表示的是優先啓動級別,而10是關閉級別,數字越低優先級越高),而後在文檔中加入export JAVA_HOME=/usr/local/java/jdk/jdk1.8.0_161 export JRE_HOME=/usr/local/java/jdk/jdk1.8.0_161/jre 指定java_home 和java_jrebash
#!/bin/bash # chkconfig: 2345 99 10 ……………… export JAVA_HOME=/usr/local/java/jdk/jdk1.8.0_161 export JRE_HOME=/usr/local/java/jdk/jdk1.8.0_161/jre
執行命令測試
chkconfig --add tomcatrest
以後就能夠使用 service tomcat start 啓動tomcat服務日誌
重啓測試是否自動啓動tomcat code