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.
[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
There are number of ways of getting the task done:
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
Use watch command to run a script after some interval to check if process is running, if not then restart the process
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.
发布评论
评论(8)
或者您可以删除 while 循环和睡眠,并将脚本放入 cron 作业中,设置为每分钟运行一次
or you could remove the while loop and sleep and put the script in a cron job set to run every minute
监视。
http://mmonit.com/monit/
monit.
http://mmonit.com/monit/
我不久前写了其中一个,名为 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.
最简单的事情就是让最初的父母进行监控。 EG,
这个脚本很容易被杀死,所以你可能想要
捕获信号,但同样的情况也适用于任何
监视你可能写的内容。你可能会
如果 cmd 终止,还想插入延迟
立即,或添加一些日志记录(调用记录器
调用 cmd 后)。没有必要花哨。
The easiest thing to do is have the initial parent do the monitoring. EG,
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.
使用您在简单的 命令 http://supervisord.org/configuration.html" rel="nofollow noreferrer">配置文件,Supervisor 可以启动、监视和重新启动意外终止的进程。
考虑
/etc/supervisor/conf.d/forever.conf
中的以下 Supervisor 配置文件片段,它每秒显示日期和时间:程序
forever
以 PID 15474 开头:永远终止该进程,Supervisor 会使用新进程 ID 15760 自动重新启动它:
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
starts with PID 15474:Kill the
forever
process and Supervisor restarts it automatically with new process ID 15760:如果您使用的是 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
有多种方法可以完成任务:
There are number of ways of getting the task done:
pgrep <process name> | wc -l
systemd 是一个复杂的进程管理器,可在大多数主要 Linux 发行版上使用。
systemd is a sophisticated process manager available on most major Linux distributions.