[英]Centos 7: Auto restart application if it stopped
I want to auto restart my application "Fiware IoT Agent" if it stopped, the problem is that it depends of Mongo Db Data Base and the Mosquitto broker.如果我的应用程序“Fiware IoT Agent”停止,我想自动重启它,问题是它取决于 Mongo Db 数据库和 Mosquitto 代理。 My OS is centOS 7
我的操作系统是centOS 7
Here is the commands that I use to launch my three application in the following order:这是我用来按以下顺序启动我的三个应用程序的命令:
*Mongo: *蒙戈:
/usr/local/iot/mongodb-linux-x86_64-3.0.5/bin/mongod --dbpath /usr/local/iot/mongodb-linux-x86_64-3.0.5/data/db$
*Mosquitto broker *蚊子经纪人
/usr/sbin/mosquitto -c /etc/iot/mosquitto.conf &
pid=$!
echo $pid > /var/run/iot/mosquitto.pid
Iot Agent:物联网代理:
than I start my application using this command比我使用这个命令启动我的应用程序
export LD_LIBRARY_PATH=/usr/local/iot/lib
/usr/local/iot/bin/iotagent -i 192.168.1.11 -p 80 -v DEBUG -d /usr/local/iot/lib -c /etc/iot/config.json
how can I start my application if it stopped known that it depends of the other two application?如果我的应用程序停止知道它依赖于其他两个应用程序,我该如何启动它? If for example Mongo DB stopped, I must be able to restart it and then to restart my application.
例如,如果 Mongo DB 停止,我必须能够重新启动它,然后重新启动我的应用程序。
CentOS 7 uses systemd. CentOS 7 使用 systemd。 You can create systemd service for each of your applications and specify dependencies between them.
您可以为每个应用程序创建 systemd 服务并指定它们之间的依赖关系。 And specify " Restart=always " for service which need to be auto restarted.
并为需要自动重启的服务指定“ Restart=always ”。
You can create your own watch dog code.您可以创建自己的看门狗代码。 When you start your application get the
pid
of the process and the pid
of mongo DB.当您启动应用程序获得
pid
的进程和pid
蒙戈DB的。
Every couple of second like 10 seconds check that the pid
of both process still exist, or you can also make the programs touch a file every couple of seconds as well then check the file modification time to see if the programs are still alive.每隔几秒(比如 10 秒)检查两个进程的
pid
是否仍然存在,或者您也可以让程序每隔几秒触摸一个文件,然后检查文件修改时间以查看程序是否仍然存在。
If the program hasn't touched the file or if you go jus the pid
route and the pid doesn't exist.如果程序没有触及文件,或者如果你只是使用
pid
路由而 pid 不存在。 Then the program has died.然后程序就死了。
Restart the program and get the new pid
and go about again in a forever while
loop.重新启动程序并获取新的
pid
并在永远的while
循环中再次运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.