温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用shell脚本怎么定时重启tomcat

发布时间:2020-12-11 14:11:16 来源:亿速云 阅读:392 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关利用shell脚本怎么定时重启tomcat,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

1、 在某个目录新建一个 .sh 脚本文件

vim tomcatStart.sh

2、 在 tomcatStart.sh 文件里面写入一下代码

#!/bin/bash
/etc/profile
tomcatPath="/usr/local/tomcat9"
binPath="$tomcatPath/bin"
echo "[info][$(date)]正在监控tomcat,路径:$tomcatPath"
pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`
if [-n "pid"]; then
echo "[info][$(date)]tomcat进程为:$pid"
echo "[info][$(date)]tomcat已经启动,准备使用shutdown命令关闭"
$binPath"/shutdown.sh"
sleep 2
pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`
if [-n "$pid"]; then
echo "[info][$(date)]使用shutdown关闭失败,准备kill进程"
kill -9 $pid
echo "[info][$(date)]kill进程完毕"
sleep 1
else
echo "[info][$(date)]使用shutdown关闭成功"
fi
else
echo "[info][$(date)]tomcat未启动"
fi
echo "[info][$(date)]准备启动tomcat"
$binPath"/startup.sh"

3、 修改 tomcatStart.sh 的权限

sudo chmod 777 tomcatStart.sh

4、 添加脚本到 crontab 定时任务

crontab -e
// 第一个是 tomcatStart.sh 的路径, 第二个是将日志输出到某个文件中
00 03 * * * /usr/local/tomcat9/bin/tomcatStart.sh >> /home/zhang/tomcatLog.txt

5、 重启一下 crontab 以生效

systemctl restart crond

关于利用shell脚本怎么定时重启tomcat就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI