为了远程启动一个程序,记录它的输出并立即看到该日志,我正在使用这个脚本:
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 &
最佳答案
好的,这个简单的变体有效:
( nohup mycommand 2>&1 & echo $! > mycommand.pid ) | tee -a server.log &
我不确定为什么 ;
不起作用以及为什么我必须改用 &
。
关于linux - 获取输出为管道的命令的 pid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26754469/