Get the pid of a command whose output is piped

試著忘記壹切 提交于 2019-12-11 08:32:54

问题


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 &

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).

If I just add echo $! > mycommand.pid I get the pid of tee.

How can I reliably write the pid of mycommand to a file ?

And by the way, why doesn't this give the right pid ?

( nohup mycommand 2>&1 ; echo $! > mycommand.pid ) | tee -a server.log &

回答1:


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.



来源:https://stackoverflow.com/questions/26754469/get-the-pid-of-a-command-whose-output-is-piped

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!