有没有一种方法可以监控一个进程,如果它已经死了,是否可以调用一个脚本,反过来又会使该进程重新启动?
答案 0 :(得分:6)
#!/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作业集中以便每分钟运行
答案 1 :(得分:2)
monit的。
答案 2 :(得分:1)
我刚才写了一篇名为relight的文章。还有更强大的解决方案,但这个很简单,对我有用。
答案 3 :(得分:1)
最简单的方法是让初始父母进行监控。 EG,
#!/bin/sh while true; do cmd # When you get here the process has died. start # the loop again and restart it done
这个脚本很容易被杀死,所以你可能会想要 陷阱信号,但任何情况都是如此 监视你可能写的。你可能会 如果cmd正在终止,也希望插入延迟 立即,或添加一些日志记录(呼叫记录器 你打电话给cmd后)。没有必要变得花哨。
答案 4 :(得分:1)
使用您在简单command中指定的configuration file,Supervisor可以启动,监控并重新启动意外死亡的流程。
考虑以下/etc/supervisor/conf.d/forever.conf
中的以下Supervisor配置文件片段,该片段每秒显示日期和时间:
[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
答案 5 :(得分:0)
如果您使用的是SysV系统(不是Upstart),您可以将进程重新生成在inittab中。
只需编辑/ etc / inittab文件并添加如下所示的行:
PROC:12345:重生:/路径/到/处理
答案 6 :(得分:0)
完成任务有多种方法:
pgrep <process name> | wc -l
答案 7 :(得分:0)
systemd是大多数主要Linux发行版上都可用的复杂流程管理器。