linux - 如果关闭时间太长,bash 会终止进程

标签 linux bash centos rhel supervisord

我有一组 supervisord 运行进程,我想使用脚本自动启动和停止。 但有时 supervisor shutdown 命令需要很长时间才能完成,等待时间开始堆积。 目前我在做:

     for each in "${args[@]}"; do
             (supervisorctl -c configs/supervisord_${each}.conf shutdown) & sleep 8s & (kill -9 `ps aux | grep supervisor| grep ${each} | a    wk '{print $2}'` && kill -9 `ps aux | grep supervisor| grep ${each} | awk '{print $2}'`)
         done

有没有更简洁的方法可以在几秒钟后终止主管进程,如果停止时间太长? 需要明确的是,我试图通过 supervisorctl -c {config path} shutdown 终止我试图关闭的主管进程,而不是 supervisorctl shutdown 命令本身。

最佳答案

好吧,如果你看一下 bash(1)(或者 sh(1),如果你使用普通的 bourne shell)你会发现有一个变量$! 存储最后一个后台执行命令的 pid (&),因此您可以将循环体替换为:

supervisorctl -c configs/supervisrod_${each}.conf shutdown &
pid_to_kill=$!
(sleep 8; kill -9 "${pid_to_kill}") &

搜索进程表是一个你应该避免的昂贵操作。还请记住,kill -9 不允许程序完全关闭,您可能会遗漏一些数据。

第二次尝试

如果你想杀死主管(而不是你用来控制它的 supervisorctl),你可以简单地检查它是否在 /var/run 中写入了一些 pid 文件目录。由于这是常见的用法,如果您需要一个不相关的进程(例如 supervisorctl,没有 parent 的父子关系)来访问它的 pid 并能够向它发送信号。

关于linux - 如果关闭时间太长,bash 会终止进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622959/

相关文章:

来自带有 *-devel 包但在 Centos 6.5 上没有调试符号的存储库的 Qt5

c# - Active Directory - 使用 C# 的用户的 UNIX 属性

python - 为什么使用 apt.Cache 而不是 apt.cache.Cache() 创建对象

linux - Bash 非阻塞源(背景???)

bash - 从文件中捕捉序列的开始

shell - 使用 shell 脚本从两个目录中查找和移动重复文件

c++ - Flutter和Linux

linux - 即时电子邮件加密/签名

python - 拦截 bash 脚本函数/系统调用并将它们包装到自定义函数中

php - Nginx 上的 Laravel : accessing to "public/" of the app results in "Permission denied" even though it has the same user as Nginx