监控流程

时间:2010-03-01 05:16:18

标签: linux process

有没有一种方法可以监控一个进程,如果它已经死了,是否可以调用一个脚本,反过来又会使该进程重新启动?

8 个答案:

答案 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)

答案 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 fileSupervisor可以启动,监控并重新启动意外死亡的流程。

考虑以下/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)

完成任务有多种方法:

  1. 根据其他人的建议 - 运行脚本以检查进程是否正在运行,如果未运行则重新启动进程。要检查流程是否正在运行,您可以使用pgrep <process name> | wc -l
  2. 使用watch command在一段时间后运行脚本以检查进程是否正在运行,如果没有则重新启动进程
  3. 创建一个父进程,该进程将始终查找子进程,如果子进程崩溃或停止,将通知父进程,然后重新启动新进程。

答案 7 :(得分:0)

systemd是大多数主要Linux发行版上都可用的复杂流程管理器。