1.创建脚本文件

创建tomcat重启的脚本文件/usr/sbin/restart_tomcat.sh

#!/bin/bash

# 引用环境变量(否则可能无法争取执行tomcat命令)
source /etc/profile

echo "==================== $(date +'%Y-%m-%d %T') 重启tomcat服务开始 ====================" >> /opt/log/restart_tomcat_log.txt

# 关闭tomcat服务
/opt/tomcat/bin/shutdown.sh  >> /opt/log/restart_tomcat_log.txt
echo "-------------------------------------------------------------------" >> /opt/log/restart_tomcat_log.txt

# 暂停5秒
ping 127.0.0.1 -c 5 >> /opt/log/restart_tomcat_log.txt
echo "-------------------------------------------------------------------" >> /opt/log/restart_tomcat_log.txt

# 启动tomcat服务
/opt/tomcat/bin/startup.sh >> /opt/log/restart_tomcat_log.txt

echo "==================== $(date +'%Y-%m-%d %T') 重启tomcat服务结束 ====================" >> /opt/log/restart_tomcat_log.txt

echo "" >> /opt/log/restart_tomcat_log.txt
echo "" >> /opt/log/restart_tomcat_log.txt
echo "" >> /opt/log/restart_tomcat_log.txt

CenterOS设置Tomcat定时重启_第1张图片

2.添加到定时任务

执行命令

[root@qfx log]# crontab -l

编辑内容

# 每周一、周五凌晨3点30分自动重启tomcat
30 3 * * 1,5 /usr/sbin/restart_tomcat.sh

CenterOS设置Tomcat定时重启_第2张图片

保存并刷新定时任务即可

[root@qfx log]# systemctl reload crond.service

定时任务可参考CentOS7安装定时任务