首页 > 系统相关 >linux下实现tomcat定时自动重启

linux下实现tomcat定时自动重启

时间:2023-09-22 14:25:16浏览次数:44  
标签:grep tomcat 重启 pid linux 定时 restart

 

tomcat自带的脚本中没有提供直接restart的模式,但是有start和shutdown两种模式。要实现restart模式,实际上只需要判断是否已经启动tomcat,若已经启动则限制性shutdown,然后在执行start。或者通过ps aux查询出是否存在tomcat进程,若存在kill -9 tomcat的pid,再进行启动tomcat.通过shell脚本实现tomcat重启,并将该脚本添加到Linux的定时任务计划中,就可以实现tomcat的定时自启动功能。

  •          以下是实现tomcat重启的shell脚本

 

复制代码
#!/bin/sh      
#初始化全局环境变量  
. /etc/profile  
  
#查找tomcat的pid  
pid=`ps aux | grep tomcat | grep -v grep | grep -v Restart | grep -v restart | awk '{print $2}'`  
echo "the tomcat pid is $pid"    
  
#判断tomcat进程是否存在  
if [ -n "$pid" ];then  
   sleep 1  
   pid=`ps aux | grep tomcat | grep -v grep | grep -v restart | grep -v Restart | awk '{print $2}'`  
   if [ -n "$pid" ]; then  
      sleep 1  
      echo "tomcat进程将被杀死."  
      kill -9 $pid  
   fi  
     
   sleep 1    
  
   echo "tomcat进程已经被杀死,先重新启动tomcat."   
   service tomcat start  
else  
    echo "tomcat进程不存在,先重新启动tomcat."  
    service tomcat start  
fi  
复制代码

 

                

     由于我原来对tomcat已经做了服务自启动,因此可以直接执行service tomcat start 启动tomcat。

 

  • 实现tomcat定时任务
vi /etc/crontab  

可以看到以下内容

 

复制代码
SHELL=/bin/bash  
PATH=/sbin:/bin:/usr/sbin:/usr/bin  
MAILTO=root  
HOME=/  
  
# For details see man 4 crontabs  
  
# Example of job definition:  
# .---------------- minute (0 - 59)  
# |  .------------- hour (0 - 23)  
# |  |  .---------- day of month (1 - 31)  
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...  
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat  
# |  |  |  |  |  
# *  *  *  *  * user-name command to be executed
复制代码

以上备注说明非常清楚,不再加以详细说明,将tomcat重启执行脚本语句添加到最后一行。

#设定每天凌晨3点整重启tomcat  
0 3 * * * root sh /home/scripts/tomcat/restart >> /home/logs/tomcat/restart.log

特别注意,在写完脚本后要enter,否则crond服务无法识别,定时任务无法生效

保存并重启crontab服务

service crond restart 

以上便完成了tomcat的定时重启功能

标签:grep,tomcat,重启,pid,linux,定时,restart
From: https://www.cnblogs.com/lcword/p/17722236.html

相关文章

  • linux 查看jdk安装路径
    [root@iz2ze9ufq5ehrayz6j88sazbin]#java-versionjavaversion"1.8.0_191"Java(TM)SERuntimeEnvironment(build1.8.0_191-b12)JavaHotSpot(TM)64-BitServerVM(build25.191-b12,mixedmode)[root@iz2ze9ufq5ehrayz6j88sazbin]#whichjava/usr......
  • crontab 定时任务
    crontab详细用法定时任务一、名词解释crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令。使用crontab你可以在指定的时间执行一个shell脚本或者一系列Linux命令。二、使用方式2.1如何添加crontab定时任务方式一:]#crontab–e#每天早上5点......
  • Linux上监控tomcat down掉后自动重启tomcat
    Linux上监控tomcatdown掉后自动重启tomcat解决思路Linux上监控tomcatdown掉后自动重启tomcat第一步编辑:monitor.sh文件(文件内容看下文)第二步安装crontab(安装步骤看下文)第三步添加定时任务:crontab-e第四部添加crontab内容*/1****/usr/java/monitor.sh第五步重新......
  • Windows和Linux中的库、对象、可执行文件后缀名
    中国软件工程师面试常问的问题Justa"Scientific"(Interview)ExplanationforInterviewProblemsforSoftwareEngineers(mostChineseInterviews)Windows和Linux中的库、对象、可执行文件后缀名library,object,executablefilessuffixnameinWindowsandLinuxL......
  • linux查看用户的所有组,和组下面有多少用户
    1.查看用户的所有组idgroups[root@ceph1~]#idtest01uid=1000(test01)gid=1001(test01)groups=1001(test01),1000(test)[root@ceph1~]#idtest02uid=1001(test02)gid=1002(test02)groups=1002(test02),1000(test)[root@ceph1~]#idtest03uid=1002(test03)gid......
  • Linux_JXNUFourWeek_Linux过滤器
    frompixivgrep行过滤grep匹配内容源输入grepandAndfile.txt//这条命令将会匹配Andfile.txt中文本的全部包含and的行grep-iandAndfile.txt//-i会忽略大小写grep-nandAndfile.txt//-n会显示出匹配出来的行的行号grep-vandAndfile.txt//-v是反向,即这里表......
  • Linux系统添加历史命令操作记录(系统设置)
    Linux系统添加历史命令操作记录原创 浩道 浩道Linux 2023-09-2207:50 发表于广东收录于合集#linux学习320个关注上方浩道Linux,回复资料,即可获取海量Linux、Python、网络通信、网络安全等学习资料!前言大家好,这里是浩道Linux,主要给大家分享Linux、Python、网络通信、......
  • linux 中 find命令 -maxdepth 和 -mindepth 选项
     001、[root@pc1dir001]#lstest01test02ww.txtxx.map[root@pc1dir001]#tree.├──test01│  ├──cc.csv│  └──kk.txt├──test02│  ├──dirxx│  │  └──diryy│  │  ├──rr.ped│  │  └......
  • 3-Linux文档查看指令,关机重启、相关知识点的拓展与总结
    一、文档的查看指令1、tail指令作用:查看一个文件的末n行语法:#tail-n文件的路径说明:-n可以不写,不写,默认表示10行。案例:新建一个1.txt文档,使用tail指令查看root/1.txt文件的末5行和末10行tail-5/root/1.txttail/root/1.txt2、head指令作用:查看文件的头n行语法:#hea......
  • linux 中同时限定多个目录进行查找
     001、不限制[root@pc1dir001]#ls##测试目录文件test01test02test03[root@pc1dir001]#tree.├──test01│  ├──cc.csv│  └──kk.txt├──test02│  ├──mm.txt│  └──yy.csv└──test03├──ee.txt└......