杀死管道进程的好方法?

发布于 2025-01-07 16:36:28 字数 501 浏览 0 评论 0原文

我想在创建 shell 时处理它的每个标准输出行。我想获取 test.sh 的输出(一个漫长的过程)。我当前的方法是这样的:

 ./test.sh >tmp.txt &
 PID=$!
 tail -f tmp.txt | while read line;  do
 echo $line
 ps ${PID} > /dev/null
 if [ $? -ne 0 ]; then
     echo "exiting.."
 fi
 done;

但不幸的是,这将打印“exiting”然后等待,因为 tail -f 仍在运行。我尝试了 breakexit

我在 FreeBSD 上运行它,所以我无法使用某些 linux tails 的 --pid= 选项。

我可以使用 ps 和 grep 来获取尾部的 pid 并杀死它,但这对我来说看起来非常难看。

有什么提示吗?

I want to process each stdout-line for a shell, the moment it is created. I want to grab the output of test.sh (a long process). My current approach is this:

 ./test.sh >tmp.txt &
 PID=$!
 tail -f tmp.txt | while read line;  do
 echo $line
 ps ${PID} > /dev/null
 if [ $? -ne 0 ]; then
     echo "exiting.."
 fi
 done;

But unfortunately, this will print "exiting" and then wait, as the tail -f is still running. I tried both break and exit

I run this on FreeBSD, so I cannot use the --pid= option of some linux tails.

I can use ps and grep to get the pid of the tail and kill it, but thats seems very ugly to me.

Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2025-01-14 16:36:28

为什么需要 tail 流程?

可以按照 or 的方式做一些事情

./test.sh | while read line; do
  # process $line
done

如果您想将输出保留在 tmp.txt 中,您

./test.sh | tee tmp.txt | while read line; do
  # process $line
done

:如果您仍然想使用中间 tail -f 进程,也许您可​​以使用命名管道(fifo) 而不是常规管道,以允许分离 tail 进程并获取其 pid:

./test.sh >tmp.txt &
PID=$!

mkfifo tmp.fifo
tail -f tmp.txt >tmp.fifo &
PID_OF_TAIL=$!
while read line; do
  # process $line
  kill -0 ${PID} >/dev/null || kill ${PID_OF_TAIL}
done <tmp.fifo
rm tmp.fifo

但是我应该提到,这样的解决方案会带来几个严重的竞争条件问题:

  • test 的 PID .sh 可以被重用另一个进程;
  • 如果当您读取最后一行时 test.sh 进程仍然存在,那么之后您将没有任何其他机会检测到它的死亡,并且您的循环将挂起。

why do you need the tail process?

Could you instead do something along the lines of

./test.sh | while read line; do
  # process $line
done

or, if you want to keep the output in tmp.txt :

./test.sh | tee tmp.txt | while read line; do
  # process $line
done

If you still want to use an intermediate tail -f process, maybe you could use a named pipe (fifo) instead of a regular pipe, to allow detaching the tail process and getting its pid:

./test.sh >tmp.txt &
PID=$!

mkfifo tmp.fifo
tail -f tmp.txt >tmp.fifo &
PID_OF_TAIL=$!
while read line; do
  # process $line
  kill -0 ${PID} >/dev/null || kill ${PID_OF_TAIL}
done <tmp.fifo
rm tmp.fifo

I should however mention that such a solution presents several heavy problems of race conditions :

  • the PID of test.sh could be reused by another process;
  • if the test.sh process is still alive when you read the last line, you won't have any other occasion to detect its death afterwards and your loop will hang.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文