有没有一种方法可以监视进程,如果它死了,是否可以调用一个脚本,从而使进程恢复?
#!/bin/bash while true do if pgrep myprocess >/dev/null ;then echo "up" else echo "down" /path/to/myprocess & fi sleep 60 done
或者你可以删除while循环和睡眠,并把脚本设置为每分钟运行的cron作业
monit的。
我前一段时间写了一个叫relight的文章 。 还有更强大的解决方案,但这个很简单,适合我。
最简单的做法是让最初的父母进行监控。 例如,
#!/ bin / sh的 而真实的; 做 CMD #当你到达这个过程已经死了。 开始 #再次循环并重新启动它 DONE
这个脚本可能会被杀死,所以你可能想要捕捉信号,但是你可能写的任何监视器也是如此。 如果cmd正在立即终止,您可能还需要插入延迟,或者添加一些日志记录(在调用cmd后调用记录器)。 没有必要变得花俏。
如果你使用的是SysV系统(不是Upstart),你可以把这个进程重新生成inittab。
只需编辑你的/ etc / inittab文件并添加一行就可以了:
PROC:12345:重生:/路径/到/处理
有很多方法来完成任务:
pgrep <process name> | wc -l
pgrep <process name> | wc -l
systemd是大多数主要Linux发行版上可用的复杂进程管理器。
使用您在简单配置文件中指定的命令 , Supervisor可以启动,监视和重新启动意外死亡的进程。
考虑在/etc/supervisor/conf.d/forever.conf
的以下Supervisor配置文件片段,它/etc/supervisor/conf.d/forever.conf
显示日期和时间:
[program:forever] command=/bin/bash -c 'while true; do echo "Current time is `date`"; sleep 1; done;'
程序forever
以PID 15474开始:
derek@derek-lubuntu:~/Projects/fire$ sudo supervisorctl status forever forever RUNNING pid 15474, uptime 0:00:17 derek@derek-lubuntu:~/Projects/fire$ sudo supervisorctl tail forever Current time is Fri Jul 7 17:11:10 EDT 2017 Current time is Fri Jul 7 17:11:11 EDT 2017 Current time is Fri Jul 7 17:11:12 EDT 2017 Current time is Fri Jul 7 17:11:13 EDT 2017 Current time is Fri Jul 7 17:11:14 EDT 2017 Current time is Fri Jul 7 17:11:15 EDT 2017 Current time is Fri Jul 7 17:11:16 EDT 2017 Current time is Fri Jul 7 17:11:17 EDT 2017 Current time is Fri Jul 7 17:11:18 EDT 2017 Current time is Fri Jul 7 17:11:19 EDT 2017 Current time is Fri Jul 7 17:11:20 EDT 2017 Current time is Fri Jul 7 17:11:21 EDT 2017 Current time is Fri Jul 7 17:11:22 EDT 2017 Current time is Fri Jul 7 17:11:23 EDT 2017 Current time is Fri Jul 7 17:11:24 EDT 2017 Current time is Fri Jul 7 17:11:25 EDT 2017
杀死forever
进程,Supervisor使用新进程ID 15760自动重新启动它:
derek@derek-lubuntu:~/Projects/fire$ sudo kill 15474 derek@derek-lubuntu:~/Projects/fire$ sudo supervisorctl status forever forever RUNNING pid 15760, uptime 0:00:02 derek@derek-lubuntu:~/Projects/fire$ sudo supervisorctl tail forever Current time is Fri Jul 7 17:11:21 EDT 2017 Current time is Fri Jul 7 17:11:22 EDT 2017 Current time is Fri Jul 7 17:11:23 EDT 2017 Current time is Fri Jul 7 17:11:24 EDT 2017 Current time is Fri Jul 7 17:11:25 EDT 2017 Current time is Fri Jul 7 17:11:26 EDT 2017 Current time is Fri Jul 7 17:11:27 EDT 2017 Current time is Fri Jul 7 17:11:28 EDT 2017 Current time is Fri Jul 7 17:11:29 EDT 2017 Current time is Fri Jul 7 17:11:30 EDT 2017 Current time is Fri Jul 7 17:11:31 EDT 2017 Current time is Fri Jul 7 17:11:32 EDT 2017 Current time is Fri Jul 7 17:11:33 EDT 2017 Current time is Fri Jul 7 17:11:34 EDT 2017 Current time is Fri Jul 7 17:11:35 EDT 2017 Current time is Fri Jul 7 17:11:36 EDT 2017