获取输出为管道的命令的pid
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).
现在,我想在一个文件中存储新启动的mycommand的pid(其名称非常通用,我不能只使用pgrep,因为存在冲突的风险)。
If I just add echo $! > mycommand.pid
I get the pid of tee
.
如果我只是添加echo $! > mycommand.pid我得到了发球的pid。
How can I reliably write the pid of mycommand
to a file ?
如何可靠地将mycommand的pid写入文件?
And by the way, why doesn't this give the right pid ?
顺便说一句,为什么这不给出正确的pid?
( nohup mycommand 2>&1 ; echo $! > mycommand.pid ) | tee -a server.log &
1 个解决方案
#1
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.
我不确定为什么;不起作用,为什么我必须使用&而不是。
更多相关文章
- Linux文件归档和解压缩
- 如何使用“tail -1 * / filename”拖尾多个文件的最后一行
- Linux文件权限概念
- 将JSON文件中的所有时间戳转换为bash(Ubuntu)中的unix时间戳
- 小记——inotify文件监控
- Linux下运行.cpp文件
- linux下如何导出.txt文件?
- Linux的五个文件查找命令:find,locate,whereis,which,type
- Re:从零开始的Linux之路(文件权限)