获取其输出通过管道传递的命令的pid [英] Get the pid of a command whose output is piped
问题描述
为了远程启动程序,请记录其输出并立即查看该日志,我正在使用此脚本:
In order to remotely start a program, log its output and immediately see that log, I'm using this script:
nohup mycommand 2>&1 | tee -a server.log &
现在,我想将新启动的mycommand的pid存储在文件中(其名称非常通用,我不能仅使用pgrep,否则会发生冲突).
Now, I would like to store in a file the pid of the newly started mycommand (whose name is very generic, I can't just use pgrep as there would be a risk of collision).
如果我只添加 echo $!>mycommand.pid
我得到 tee
的pid.
If I just add echo $! > mycommand.pid
I get the pid of tee
.
如何可靠地将 mycommand
的pid写入文件?
How can I reliably write the pid of mycommand
to a file ?
顺便说一句,为什么没有给出正确的pid?
And by the way, why doesn't this give the right pid ?
( nohup mycommand 2>&1 ; echo $! > mycommand.pid ) | tee -a server.log &
推荐答案
好的,这个简单的变体可以工作:
OK, this simple variant works :
( nohup mycommand 2>&1 & echo $! > mycommand.pid ) | tee -a server.log &
我不确定;
为何不起作用,以及为什么我必须使用&
代替.
I'm not sure why the ;
didn't work and why I have to use &
instead.
这篇关于获取其输出通过管道传递的命令的pid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!