如何在Linux操作系统定时重启Tomcat服务?
背景:
正常情况下启动和关闭tomcat程序命令如下:
/application/tomcat/bin/startup.sh/application/tomcat/bin/shutdown.sh
但是定时任务情况下无法启动tomcat程序,提示没有java环境变量,提示crontab中不需要加/bin/sh /application/tomcat/bin/startup.sh因为tomcat程序本身就是可执行文件,如果本身不是可执行文件
需要加/bin/bash。
具体定时任务和报错日志如下:
[root@szxjdw01-tomcat-50 scripts]# crontab -e#time sync by jeremy at 2018-08-13*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&114 09 * * * /application/tomcat/bin/shutdown.sh >>/tmp/tomcat.log01 00 * * * /application/tomcat/bin/startup.sh >>/tmp/tomcat.log
日志报错提示没有java环境变量[root@szxjdw01-tomcat-50 scripts]# cat /tmp/tomcat.logNeither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least one of these environment variable is needed to run this programNeither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least one of these environment variable is needed to run this programNeither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least one of these environment variable is needed to run this program
那么如何才能在linux系统中定时重启tomcat服务呢?请看如下:
Linux操作系统定时重启Tomcat服务的方法如下:
1、编写tomcat_shutdown.sh:
[root@szxjdw01-tomcat-50 ~]# cat /server/scripts/tomcat_shutdown.sh #!/bin/bash#set java environmentexport JAVA_HOME=/usr/java/jdkexport CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/libexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATHecho "========begin stop the tomcat========"'/application/tomcat/bin/shutdown.sh'
2、编写tomcat_startup.sh:
[root@szxjdw01-tomcat-50 ~]# cat /server/scripts/tomcat_startup.sh #!/bin/bash#set java environmentexport JAVA_HOME=/usr/java/jdkexport CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/libexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATHecho "========begin stop the tomcat========"'/application/tomcat/bin/startup.sh'
3、测试脚本是否ok
[root@szxjdw01-tomcat-50 ~]# cd /server/scripts/[root@szxjdw01-tomcat-50 scripts]# netstat -lntup|grep java #查看当前tomcat监听的端口 tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 4055/java tcp 0 0 :::8009 :::* LISTEN 4055/java tcp 0 0 :::8080 :::* LISTEN 4055/java [root@szxjdw01-tomcat-50 scripts]# lltotal 12drwxr-x---. 2 root root 4096 Sep 18 10:17 logs-rw-r--r--. 1 root root 262 Sep 18 10:15 tomcat_shutdown.sh-rw-r--r--. 1 root root 261 Sep 18 10:16 tomcat_startup.sh[root@szxjdw01-tomcat-50 scripts]# sh tomcat_shutdown.sh #关闭tomcat服务========begin stop the tomcat========Using CATALINA_BASE: /application/tomcatUsing CATALINA_HOME: /application/tomcatUsing CATALINA_TMPDIR: /application/tomcat/tempUsing JRE_HOME: /usr/java/jdkUsing CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar[root@szxjdw01-tomcat-50 scripts]# netstat -lntup|grep java #继续查看tomcat服务没有监听任务端口了 [root@szxjdw01-tomcat-50 scripts]# sh tomcat_startup.sh #再次启动tomcat服务========begin stop the tomcat========Using CATALINA_BASE: /application/tomcatUsing CATALINA_HOME: /application/tomcatUsing CATALINA_TMPDIR: /application/tomcat/tempUsing JRE_HOME: /usr/java/jdkUsing CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jarTomcat started.[root@szxjdw01-tomcat-50 scripts]# netstat -lntup|grep java #再次查看发现tomcat端口8080已启动了,一般情况下 tomcat的8005端口会延迟启动,稍等1-2分钟后才启动8005端口 tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 4221/java tcp 0 0 :::8009 :::* LISTEN 4221/java tcp 0 0 :::8080 :::* LISTEN 4221/java
4、在linux系统终端中,输入命令“crontab -e”:
在vi操作界面,输入:
[root@szxjdw01-tomcat-50 ~]# crontab -e#time sync by jeremy at 2018-08-13*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&101 13 * * * /bin/bash /server/scripts/tomcat_shutdown.sh >>/tmp/tomcat.log02 13 * * * /bin/bash /server/scripts/tomcat_startup.sh >>/tmp/tomcat.log
实时查看日志:
[root@szxjdw01-tomcat-50 ~]# tailf /tmp/tomcat.log========begin stop the tomcat================begin stop the tomcat========Tomcat started.
端口也是从无到有了
[root@szxjdw01-tomcat-50 scripts]# netstat -lntup|grep java #tomcat服务启动时,查看当前tomcat监听的端口 tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 4055/java tcp 0 0 :::8009 :::* LISTEN 4055/java tcp 0 0 :::8080 :::* LISTEN 4055/java [root@szxjdw01-tomcat-50 scripts]# netstat -lntup|grep java #tomcat服务关闭后,查看tomcat监听端口没有了 [root@szxjdw01-tomcat-50 scripts]# netstat -lntup|grep java #tomcat服务启动时后,再次查看发现tomcat端口8080已启动了,一般情况下 tomcat的8005端口会延迟启动,稍等1-2分钟后才启动8005端口 tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 4221/java tcp 0 0 :::8009 :::* LISTEN 4221/java tcp 0 0 :::8080 :::* LISTEN 4221/java
如上说明定时任务重启tomcat服务ok了。
5、使用“crontab -l”命令,可以查看定时任务的设置情况。
[root@szxjdw01-tomcat-50 scripts]# crontab -l#time sync by jeremy at 2018-08-13*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&155 10 * * * /bin/bash /server/scripts/tomcat_shutdown.sh >>/tmp/tomcat.log56 10 * * * /bin/bash /server/scripts/tomcat_startup.sh >>/tmp/tomcat.log
6、如果定时任务不执行可以通过查看日志:
[root@szxjdw01-tomcat-50 mail]# less /var/log/cronSep 18 10:55:01 localhost CROND[4449]: (root) CMD (/bin/bash /server/scripts/tomcat_shutdown.sh >>/tmp/tomcat.log)Sep 18 10:55:36 localhost crontab[4497]: (root) LIST (root)Sep 18 10:55:37 localhost crontab[4499]: (root) LIST (root)Sep 18 10:55:37 localhost crontab[4501]: (root) LIST (root)Sep 18 10:55:51 localhost crontab[4503]: (root) LIST (root)Sep 18 10:55:52 localhost crontab[4505]: (root) LIST (root)Sep 18 10:56:01 localhost CROND[4511]: (root) CMD (/bin/bash /server/scripts/tomcat_startup.sh >>/tmp/tomcat.log)