Linux每天定时重启Tomcat服务

来源:互联网 发布:ubuntu 12.04 源 编辑:程序博客网 时间:2024/04/27 12:19
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:手动测试文件是否可以执行

[root@UCOM002 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

[root@UCOM002 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任务计划

[root@UCOM002 bin]# crontab -e

0 1 * * *  /opt/apache-tomcat-9.0.0.M11/bin/restart_tomcat.sh

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

5:crontab任务不执行,手工执行文件可以

问题描述:默认是root用户登录

sh文件写法是

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

#!/bin/sh
./etc/profile
sh /opt/apache-tomcat-9.0.0.M11/bin/shutdown.sh
sleep 120s
sh /opt/apache-tomcat-9.0.0.M11/bin/startup.sh

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

结果还是不执行

于是从根目录/切换到root用户目录~,查看环境变量文件

[root@UCOM002 /]# su - root
[root@UCOM002 ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
[root@UCOM002 ~]#

 

发现export PATH下面没有定义环境变量信息,导致root用户没有环境变量信息,所以

在root用户下面执行./etc/profile无效

于是在export PATH 下面添加两行,完整文件如下

[root@UCOM002 /]# su - root
[root@UCOM002 ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
export JAVA_HOME=/usr/java/jdk1.8.0_112
export CATALINA_HOME=/opt/apache-tomcat-9.0.0.M11
然后保存root用户下对.bash_profile文件的修改退出,再次编辑crontab 任务

最好是重启crond服务,给一个任意时间,计划被执行

* 1 * * *  /restart_test.sh

 查看crond记录

[root@UCOM002 ~]# tail -f /var/log/cron
Dec 2 14:01:01 UCOM002 run-parts(/etc/cron.hourly)[791]: finished 0anacron
Dec 2 14:01:01 UCOM002 run-parts(/etc/cron.hourly)[782]: starting mcelog.cron
Dec 2 14:01:01 UCOM002 run-parts(/etc/cron.hourly)[805]: finished mcelog.cron
Dec 2 14:10:01 UCOM002 CROND[822]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Dec 2 14:12:57 UCOM002 crontab[855]: (root) BEGIN EDIT (root)
Dec 2 14:13:29 UCOM002 crontab[855]: (root) REPLACE (root)
Dec 2 14:13:29 UCOM002 crontab[855]: (root) END EDIT (root)
Dec 2 14:13:32 UCOM002 crontab[859]: (root) LIST (root)
Dec 2 14:14:01 UCOM002 crond[6671]: (root) RELOAD (/var/spool/cron/root)
Dec 2 14:14:01 UCOM002 CROND[870]: (root) CMD (/restart_test.sh)

原创粉丝点击