【问题标题】:Get the pid of a command whose output is piped获取通过管道输出的命令的 pid
【发布时间】:2014-11-05 10:11:14
【问题描述】:

为了远程启动一个程序,记录它的输出并立即看到该日志,我正在使用这个脚本:

nohup mycommand 2>&1 | tee -a server.log &

现在,我想将新启动的 mycommand 的 pid 存储在一个文件中(其名称非常通用,我不能只使用 pgrep,因为会有冲突的风险)。

如果我只添加echo $! > mycommand.pid,我会得到tee 的pid。

如何可靠地将mycommand 的 pid 写入文件?

顺便说一句,为什么这不能给出正确的 pid ?

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

【问题讨论】:

标签: linux bash pid


【解决方案1】:

好的,这个简单的变体有效:

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

我不确定为什么 ; 不起作用以及为什么我必须改用 &

【讨论】:

  • 我刚刚接受了我的回答,因为已经 3 个月了。但是,如果有人写了一个更好的答案并给出了可靠的解释,我会很乐意接受。
猜你喜欢
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 2018-12-27
  • 2021-11-07
相关资源
最近更新 更多