linux - 获取输出为管道的命令的 pid

标签 linux bash pid

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

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/

相关文章:

windows-xp - 识别 DNS​​ 请求的 PID 源 (Windows XP)

linux - 在屏幕上打印 stdout 和 stderr 并将它们重定向到不同的文件

c++ - 使用 std::map 给出警告:取消引用指针 ‘<anonymous>’ 确实违反了严格别名规则

python - 为什么从包中全局安装 Django 不起作用? (通过python调用)

Bash 脚本 "Usage"输出格式

linux - 仅在 alpine linux 上出现算术语法错误

linux - 从 shell 到 docker 容器执行多个命令

macos - 如何以编程方式从 10.6 上的进程 ID 获取 OSX 窗口 ID? (使用 Applescript、Objective-C 等)

Python + Linux : How to determine cron job is scheduled?

linux - Elasticsearch 1.6 在 ubuntu pid 文件上重启