gpt4 book ai didi

bash - inotifywait 没有被 supervisorctl stop 杀死

转载 作者:行者123 更新时间:2023-11-29 09:25:05 33 4
gpt4 key购买 nike

我有这个 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 时,您缺少将终止信号传播到所有子进程的选项。通常一个人有一个父进程和一个子进程,当父进程得到 SIGTERM/SIGINT 它将/应该有一个协调的方式并且应该是负责发送信号的那个或者其他方式关闭 child 。

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.

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

还要记住传播 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

关于bash - inotifywait 没有被 supervisorctl stop 杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734494/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com