欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot调用 Shell 脚本实现看门狗功能

程序员文章站 2024-01-24 10:30:58
需要实现看门狗功能,定时检测另外一个程序是否在运行,使用 crontab 仅可以实现检测程序是否正在运行,无法做到扩展,如:手动重启、程序升级(如果只需要实现自动升级功能可以使用 inotify)等功...

需要实现看门狗功能,定时检测另外一个程序是否在运行,使用 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;

 @autowired
 resourceloader resourceloader;

 @override
 public void run(string... args) throws exception {
 generatefile(shellproperties.getstartupfilename());
 generatefile(shellproperties.getrestartfilename());
 }

 private void generatefile(string filename) throws ioexception {

 string filefullname = shellproperties.getfullname(filename);
 file file = new file(filefullname);
 if(file.exists()) {
 return;
 }

 // 如果文件已存在,filewriter 会先删除再新建
 filewriter filewriter = new filewriter(filefullname);

 resource resource = resourceloader.getresource("classpath:" + filename);
 inputstream inputstream = resource.getinputstream();
 inputstreamreader inputstreamreader = new inputstreamreader(inputstream);
 bufferedreader bufferedreader = new bufferedreader(inputstreamreader);

 string data;
 while ((data = bufferedreader.readline()) != null) {
 filewriter.write(data + "\n");
 }

 bufferedreader.close();
 inputstreamreader.close();
 inputstream.close();

 filewriter.close();

 // 设置权限,否则会报 permission denied
 file.setreadable(true);
 file.setwritable(true);
 file.setexecutable(true);
 }
}

2.2 定时任务定时任务执行脚本

@component
public class shelltask {

 private static final logger logger = loggerfactory.getlogger(shelltask.class);

 @autowired
 private shellproperties shellproperties;

 @scheduled(cron = "0/10 * * * * ? ")
 public void start() throws ioexception {
 executeshell(shellproperties.getstartupfilename());
 }

 private void executeshell(string filename) throws ioexception {

 string filefullname = shellproperties.getfullname(filename);
 file file = new file(filefullname);
 if(!file.exists()) {
 logger.error("file {} not existed!", filefullname);
 return;
 }

 processbuilder processbuilder = new processbuilder(filefullname);
 processbuilder.directory(new file(shellproperties.getdirectory()));

 process process = processbuilder.start();

// string input;
// bufferedreader stdinput = new bufferedreader(new inputstreamreader(process.getinputstream()));
// bufferedreader stderror = new bufferedreader(new inputstreamreader(process.geterrorstream()));
// while ((input = stdinput.readline()) != null) {
// logger.info(input);
// }
// while ((input = stderror.readline()) != null) {
// logger.error(input);
// }

 int runningstatus = 0;
 try {
 runningstatus = process.waitfor();
 } catch (interruptedexception e) {
 logger.error("shell", e);
 }

 if(runningstatus != 0) {
 logger.error("failed.");
 }else {
 logger.info("success.");
 }
 }
}

2.3 扩展

本例只实现了定时检测程序是否运行,如果没有运行则启动程序;如有需要可以添加接口,调用接口重启程序;或者添加定时任务定时检测程序是否有更新,如果有更新则下载新的 jar 包然后重启程序

看门狗程序自己可以使用 crontab 定时检测是否正在运行,模仿上面的启动脚本编写看门狗的启动脚本,然后添加定时任务:

crontab -e
*/10 * * * * /project/watchdog/watchdog.sh
sudo systemctl reload crond.service

完整代码:github

参考:

总结

到此这篇关于spring boot调用 shell 脚本实现看门狗功能的文章就介绍到这了,更多相关spring boot 看门狗内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!