linux 程序定时重启tomcat,Linux每天定时重启Tomcat服务(示例代码)

1:查看crond 服务状态(确认Linux任务计划服务开启)

service crond status

crond (pid  1937) is running...

2:编写重启Tomcat的sh可执行文件restart_tomcat.sh

#!/bin/sh

#./etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_112

sh  /opt/apache-tomcat-9.0.0.M11/bin/shutdown.sh

sleep 60s

sh  /opt/apache-tomcat-9.0.0.M11/bin/startup.sh

3:手动测试文件是否可以执行

[[email protected] bin]# ./restart_tomcat.sh

-bash: ./restart_tomcat.sh: /bin/sh^M: bad interpreter: No such file or directory

上面报错是因为restart_tomcat.sh是在window系统下直接创建的,需要修改文件的格式

为Linux系统的格式,依次执行下面的命令:

---------------------------------------------------------------------------------------------------------

给文件赋权限

chmod 777 restart_tomcat.sh

然后修改文件格式vi restart_tomcat.sh

利用如下命令查看文件格式:set ff 或 :set fileformat

可以看到如下信息

fileformat=dos 或 fileformat=unix

利用如下命令修改文件格式

:set ff=unix 或 :set fileformat=unix

保存退出

:wq

执行完上面的操作,再次执行发现执行OK

[[email protected] bin]# ./restart_tomcat.sh

Using CATALINA_BASE:   /opt/apache-tomcat-9.0.0.M11

Using CATALINA_HOME:   /opt/apache-tomcat-9.0.0.M11

Using CATALINA_TMPDIR: /opt/apache-tomcat-9.0.0.M11/temp

Using JRE_HOME:        /usr/java/jdk1.8.0_112

Using CLASSPATH:

/opt/apache-tomcat-9.0.0.M11/bin/bootstrap.jar:/opt/apache-tomcat-9.0.0.M11/bin/tomcat-juli.jar

Using CATALINA_BASE:   /opt/apache-tomcat-9.0.0.M11

Using CATALINA_HOME:   /opt/apache-tomcat-9.0.0.M11

Using CATALINA_TMPDIR:

/opt/apache-tomcat-9.0.0.M11/temp

Using JRE_HOME:        /usr/java/jdk1.8.0_112

Using CLASSPATH:

/opt/apache-tomcat-9.0.0.M11/bin/bootstrap.jar:/opt/apache-tomcat-9.0.0.M11/bin/tomcat-juli.jar

Tomcat started.

4:添加Crontab任务计划

[[email protected] bin]# crontab -e

0 1 * * *

/opt/apache-tomcat-9.0.0.M11/bin/restart_tomcat.sh

每天凌晨1点执行指定路径下的restart_tomcat.sh文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值