inotifywait 没有被 supervisorctl stop 杀死

inotifywait is not killed with supervisorctl stop

我有这个 bash 脚本 运行 主管

#!/bin/bash
DIR="/home/files"
while read file; do
   IFS=', ' read -r -a array <<< "$file"
   echo "Time: ${array[0]}"
   echo "File: ${array[1]}"
   #... doing something with the file
done < <(inotifywait -m -r -e close_write  "$DIR" --timefmt "%d_%b_%Y" --format "%T %w%f")

它运行得很好,但是当我执行 supervisorctl stop all 时,即使程序停止,inotifywait 进程仍保持 运行。一旦 bash 脚本退出,有没有办法杀死 inotifywait

编辑 1 该程序的主管配置是

[program:watch]
command=/root/watch_data.sh
user=root
stderr_logfile=/var/log/supervisord/watch_cloud.log
stdout_logfile=/var/log/supervisord/watch_cloud.log
autorestart=true
stopsignal=INT

我认为您在执行 supervisorctl stop all 时缺少将终止信号传播到所有 child 进程的选项。通常一个人有一个parent进程和children,当parent得到SIGTERM/SIGINT它will/should有一个协调的方式,应该是负责发送信号或通过其他方式关闭 children.

的人

supervisord 只是为此提供了一个选项。来自 official documentation

stopasgroup

If true, the flag causes supervisor to send the stop signal to the whole process group and implies killasgroup is true. This is useful for programs, such as Flask in debug mode, that do not propagate stop signals to their children, leaving them orphaned.

killasgroup

If true, when resorting to send SIGKILL to the program to terminate it send it to its whole process group instead, taking care of its children as well, useful e.g with Python programs using multiprocessing.

当 Supervisor 诉诸发送 SIGKILL 时,killasgroup 会终止组中的所有进程。这个想法是,当进程运行良好时,应该允许它自己处理停止它的 children。但是当它行为不端并且需要一个选项来强制整个进程组终止时。

还要记住传播 SIGINT,尽管默认操作是正常终止,但进程可以忽略。如果您不太担心脚本的正常终止,您可以传递 SIGKILL,它相当于 Linux 中的 kill -9 生成的信号。

所以你的主管配置文件中的以下选项

[program:watch]
command=/root/watch_data.sh
user=root
stderr_logfile=/var/log/supervisord/watch_cloud.log
stdout_logfile=/var/log/supervisord/watch_cloud.log
stopasgroup=true
killasgroup=true
stopsignal=KILL