有没有一种方法可以监视进程,如果它死了,是否可以调用脚本,这又会使进程恢复备份?监视进程
监视进程
回答
我之前写过一个叫做relight的人。还有更强大的解决方案,但这个很简单,适用于我。
monit。
谢谢:)看起来它对我来说是完美的。 – 2010-03-01 06:36:03
monit不会为一段时间内的过程提供图表/统计数据吗? – 2013-05-06 04:56:37
只是一个指针。 mmonit附带AGPL许可证 – madhairsilence 2017-05-22 06:33:32
#!/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作业是拥有初始父做监控。 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之后调用日志记录器 ),您可能会想要插入延迟 。没有必要变得花俏。
如果你使用的是SysV系统(不是Upstart),你可以把这个进程重新生成到inittab。
只需编辑/ etc/inittab文件,并添加这样一行:
PROC:12345:重生:/路径/到/过程
有中得到完成任务的方法数:
- 正如其他人所建议的 - 运行脚本来检查进程是否正在运行,如果没有运行,请重新启动进程。要检查你,如果进程正在运行与否,你可以使用
pgrep <process name> | wc -l
- 使用watch command一定的时间间隔后运行一个脚本来检查,如果进程正在运行,如果没有则重新启动该进程
- 创建一个父过程中,总是会对于子进程,如果子进程崩溃或停止,则会通知父进程,然后重新启动新进程。
systemd是大多数主要Linux发行版中的高级流程管理器。
使用在简单的configuration file中指定的command,Supervisor可以启动,监视和重新启动意外死亡的进程。
考虑/etc/supervisor/conf.d/forever.conf
以下主管配置文件片段,其显示的日期和时间每秒:forever
与PID 15474开始
[program:forever]
command=/bin/bash -c 'while true; do echo "Current time is `date`"; sleep 1; done;'
程序:
[email protected]:~/Projects/fire$ sudo supervisorctl status forever
forever RUNNING pid 15474, uptime 0:00:17
[email protected]:~/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
过程和主管重新启动它会自动使用新的进程ID 15760:
[email protected]:~/Projects/fire$ sudo kill 15474
[email protected]:~/Projects/fire$ sudo supervisorctl status forever
forever RUNNING pid 15760, uptime 0:00:02
[email protected]:~/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
欢迎您提供解决方案的链接,但请确保您的答案在没有它的情况下很有用:[在链接附近添加上下文](// meta.stackexchange.com/a/8259),以便您的同行用户拥有一些想法是什么以及它为什么在那里,然后引用您链接到的页面中最相关的部分,以防目标页面不可用。 [答案只是一个链接可能会被删除。](// stackoverflow.com/help/deleted-answers) – 2017-07-06 20:25:26
ps,对于自动评论感到抱歉,但它是一个非常简单的链接回答,我不是确定问题可以被关闭(请求链接)。 – 2017-07-06 20:26:03
我扩大了我的答案。我现在认为这是总体上最好的答案之一。然而,即使在最初的形式下,我的回答也不比推荐Monit的内容更少(参见stackoverflow.com/a/2354030/107158)。 – 2017-07-07 21:23:50
- 1. .NET进程监视器
- 2. JMX - 监视进程存在
- 3. PHP监视子进程
- 4. 设计用于监视和重新启动进程的监视进程
- 5. 监视Rsync进度
- 6. 用while(true)循环监视进程
- 7. 如何在Android上监视进程
- 8. 监视进程的启动和关闭?
- 9. 药剂进程监视::EXIT VS:DOWN
- 10. Java Web应用程序:监视进度
- 11. 在系统中监视进程启动
- 12. 特定进程的API监视
- 13. 如何在Mac中监视进程
- 14. 我如何监视cfthread进程?
- 15. 如何监视使用c#的进程?
- 16. Windows Server 2003上的进程监视器
- 17. 监视线程
- 18. 监视BufferedInputStream下载进度
- 19. 流的进度监视器
- 20. 监控多进程
- 21. .net线程监视
- 22. 线程监视C#
- 23. 监视.NET线程
- 24. Dropzone.js上传后进程文件和监视进度
- 25. 监控远程进程
- 26. 隐藏进程监视器应用程序窗口
- 27. 为进程管理提供心跳监视的Linux程序?
- 28. 如何在Windows中监视进程/程序的执行?
- 29. 如何监视PHP Laravel Web应用程序进程?
- 30. 在Python中监控进程?
你可能会得到更好的答案在这serverpen.com – Gabe 2010-03-01 05:19:55
https://serverfault.com/questions/637470/monitor-a-process-using-monit-in-ubuntu – quadruplebucky 2017-07-06 20:53:33
而不是把一个问题“搁置为脱离主题“,为什么不把它移动到它所属的超级用户或服务器故障? – 2017-07-07 21:26:36