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文件