如何将SIGKILL发送到整个管道?

Modified on: Sat, 30 Jun 2018 09:31:30 +0800
while true; do 
    # process substitution instead of usual pipeline to avoid waiting for all programs
    handle_input_with_timeout < <( prog1 | prog2 | prog3 )
    echo "Data stopped flowing. Trying again"
    # perminate stuck programs in previous pipeline, then try again
    sleep 5
done

如何可靠地摆脱可能卡住的prog1,prog2和prog3并保留重新尝试所需的资源?

可以单独使用Bash还是我需要使用cgroup?

作者:,Vi.

最佳答案

这可以通过流程组(建议此处)和setsid来启动之一:

while true; do 

    handle_input_with_timeout < <( setsid bash -c '
          printf -- "-$$" > /tmp/saved_process_group.pid
          prog1 | prog2 | prog3
          ')
    echo "Data stopped flowing. Trying again"
    kill -9 $(< /tmp/saved_process_group.pid )

    sleep 5
done
作者:Community,Vi.

相关问答

添加新评论