监控进程

发布于 2024-08-23 11:18:54 字数 1549 浏览 6 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

无可置疑 2024-08-30 11:18:54
#!/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 作业中,设置为每分钟运行一次

#!/bin/bash
while true
do
  if pgrep myprocess  >/dev/null ;then
     echo "up"
  else
     echo "down"
     /path/to/myprocess &
  fi
  sleep 60
done

or you could remove the while loop and sleep and put the script in a cron job set to run every minute

浅浅淡淡 2024-08-30 11:18:54

我不久前写了其中一个,名为 relight。还有更强大的解决方案,但这个很简单并且适合我。

I wrote one of these a while ago called relight. There also exist more robust solutions, but this one is simple and works for me.

佼人 2024-08-30 11:18:54

最简单的事情就是让最初的父母进行监控。 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 后)。没有必要花哨。

The easiest thing to do is have the initial parent do the monitoring. EG,

#!/bin/sh

while true; do
  cmd
  # When you get here the process has died.  start
  # the loop again and restart it
done

This script is liable to be killed so you might want
to trap signals, but the same will be true of any
monitor that you might write. You will probably
also want to insert a delay if cmd is terminating
immediately, or add some logging (call logger
after you call cmd). There's no need to get fancy.

溺深海 2024-08-30 11:18:54

使用您在简单的 命令 http://supervisord.org/configuration.html" rel="nofollow noreferrer">配置文件,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

永远终止该进程,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

Using a command that you specify in a simple configuration file, Supervisor can start, monitor, and restart a process that unexpectedly dies.

Consider the following Supervisor configuration file fragment in /etc/supervisor/conf.d/forever.conf which displays the date and time every second:

[program:forever]
command=/bin/bash -c 'while true; do echo "Current time is `date`"; sleep 1; done;'

Program forever starts with 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

Kill the forever process and Supervisor restarts it automatically with new process 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
煮茶煮酒煮时光 2024-08-30 11:18:54

如果您使用的是 SysV 系统(不是 Upstart),您可以将进程 do respawn 放在 inittab 中。

只需编辑 /etc/inittab 文件并添加如下行:

proc:12345:respawn:/path/to/process

If you are using SysV system (not Upstart), you can put the process do respawn at inittab.

Just edit your /etc/inittab file and add a line like this:

proc:12345:respawn:/path/to/process

薄情伤 2024-08-30 11:18:54

有多种方法可以完成任务:

  1. 正如其他人所建议的 - 运行脚本来检查进程是否正在运行,如果没有运行则重新启动进程。要检查进程是否正在运行,可以使用 pgrep | wc -l
  2. 使用 watch 命令 运行脚本间隔一段时间检查进程是否正在运行,如果没有则重新启动进程
  3. 创建一个父进程,它将始终寻找子进程,如果子进程崩溃或停止,将通知父进程,然后重新启动新进程。

There are number of ways of getting the task done:

  1. As suggested by others - Run a script to check if process is running, restart the process if not running. To check you if process is running or not you can use pgrep <process name> | wc -l
  2. Use watch command to run a script after some interval to check if process is running, if not then restart the process
  3. Create a parent process, that will always look for the child process, if child process crashes or stops, parent will be notified, then restarts new process.
仅冇旳回忆 2024-08-30 11:18:54

systemd 是一个复杂的进程管理器,可在大多数主要 Linux 发行版上使用。

systemd is a sophisticated process manager available on most major Linux distributions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文