Linux每天定时重启Tomcat服务
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)
最新文章
- 二叉树的创建和遍历(C版和java版)
- 读《C#高级编程》第1章问题
- angular $scope对象
- C语言中的union
- Xcode5.1离线下载安装及使用iOS5模拟器进行开发调试的方法
- HTML5 的绘图支持- canvas
- 从汇编看c++初始化列表初始化成员变量
- ThinkPHP第十五天(setField、setInc、setDec、关联模型)
- NHibernate从入门到精通系列(2)——NHibernate环境与结构体系
- MySQL之存储过程和函数
- django之全局默认设置查看及admin语言设置
- Visual Studio 2013编辑器+SourceTree代码管理工具及扩展工具
- Delphi中For In 语法应用实例
- FAFU 1136 最长递增子序列
- [COGS 2066]七十与十七
- 【Android】21.4 图片动画缩放示例
- vim多行注释和取消注释 Ubuntu
- git笔记记录
- x-forwarded-for之深度挖掘
- [1-5] 把时间当做朋友(李笑来)Chapter 5 【小心所谓成功学】 摘录
热门文章
- archlinux 安装mysql-workbench
- 如何查看MySQL执行计划
- 10个经典的C语言面试基础算法及代码
- [SmartFoxServer概述]SFS2X特点
- alert()、confirm()和prompt()的区别
- PHP会话Session
- Mono、Unity和Xamarin三者关系
- beetle 2.7海量消息广播测试
- asp.net froms 移动平台(iphone 微信)无法存储的解决办法。
- [51单片机] EEPROM AT24c02 [存储\读取一个字节]