如何获取通过管道输送到猛砸的另一个进程的PID? [英] How to get the PID of a process that is piped to another process in Bash?

查看:89
本文介绍了如何获取通过管道输送到猛砸的另一个进程的PID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在Bash中一个简单的日志服务器。它应该采取一个文件作为参数,并服务于它的netcat端口上。

I am trying to implement a simple log server in Bash. It should take a file as a parameter and serve it on a port with netcat.

( tail -f $1 & ) | nc -l -p 9977

但问题是,当netcat的终止,尾部留下运行。 (澄清:如果我没有叉尾过程中,它会永远继续运行,即使在netcat的终止)

But the problem is that when the netcat terminates, tail is left behind running. (Clarification: If I don't fork the tail process it will continue to run forever even the netcat terminates.)

如果我不知怎么知道了尾巴的PID然后我可以事后杀死它。

显然,使用$!将返回的netcat的PID。

If I somehow know the PID of the tail then I could kill it afterwards.
Obviously, using $! will return the PID of netcat.

我怎样才能得到尾巴进程的PID?

How can I get the PID of the tail process?

推荐答案

写尾部的PID文件描述符3,然后从那里捕捉到它。

Write tail's PID to file descriptor 3, and then capture it from there.

( tail -f $1 & echo $! >&3 ) 3>pid | nc -l -p 9977
kill $(<pid)

这篇关于如何获取通过管道输送到猛砸的另一个进程的PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆