没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Spring Boot调用调用 Shell 脚本实现看门狗功能脚本实现看门狗功能
主要介绍了Spring Boot调用 Shell 脚本实现看门狗功能,本文通过实例代码给大家介绍的非常详细,对大家的学
习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
需要实现看门狗功能,定时检测另外一个程序是否在运行,使用 crontab 仅可以实现检测程序是否正在运行,无法做到扩展,
如:手动重启、程序升级(如果只需要实现自动升级功能可以使用 inotify)等功能;最后决定使用 Spring Boot 调用 Shell 脚本
来实现
一、脚本一、脚本
1.1 启动脚本
#!/bin/bash
ps -ef | grep "demo-app-0.0.1-SNAPSHOT.jar" | grep -v "grep"
if [ "$?" -eq 0 ]
then
# sleep
echo $(date "+%Y-%m-%d %H:%M:%S") "process already started!"
else
nohup java -jar -server /project/watchdog/demo-app-0.0.1-SNAPSHOT.jar &
echo $(date "+%Y-%m-%d %H:%M:%S") "process has been started!"
fi
1.2 重启脚本
#!/bin/bash
pid=`ps -ef | grep "demo-app-0.0.1-SNAPSHOT.jar" | grep -v "grep" | awk '{print $2}'`
for id in $pid
do
kill -9 $id
echo "killed $id"
done
nohup java -jar -server /project/watchdog/demo-app-0.0.1-SNAPSHOT.jar &
echo $(date "+%Y-%m-%d %H:%M:%S") "process has been restarted!"
二、功能实现二、功能实现
将脚本放置在程序的资源目录下,每次程序启动时将脚本读取到指定位置,然后再通过定时任务执行脚本
配置内容:
shell:
directory: /project/watchdog
startupFileName: startup.sh
restartFileName: restart.sh
@Configuration
@ConfigurationProperties(prefix = "shell")
public class ShellProperties {
private String directory;
private String startupFileName;
private String restartFileName;
/* getter & setter */
public String getFullName(String fileName) {
return directory + File.separator + fileName;
}
}
2.1 启动时将脚本读取到指定位置
@Component
public class InitRunner implements CommandLineRunner {
@Autowired
private ShellProperties shellProperties;
资源评论
weixin_38659374
- 粉丝: 0
- 资源: 966
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功