bash 通过 ssh 获取以 nohup 启动的进程的 PID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12647382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the PID of a process started with nohup via ssh
提问by Daniel Jung
I want to start a process using nohup on a remote machine via ssh. The problem is how to get the PID of the process started with nohup, so the "process actually doing something", not some outer shell instance or the like. Also, I want to store stdout and stderr in files, but that is not the issue here...
我想通过 ssh 在远程机器上使用 nohup 启动一个进程。问题是如何使用 nohup 获取进程的 PID,因此“进程实际上在做某事”,而不是某个外壳实例等。另外,我想将 stdout 和 stderr 存储在文件中,但这不是这里的问题......
Locally, it works flawlessly using
在本地,它可以完美地使用
nohup sleep 30 > out 2> err < /dev/null & echo $!
It is echoing me the exact PID of the command "sleep 30", which I can also see using "top" or "ps aux|grep sleep".
它向我回显了命令“sleep 30”的确切 PID,我也可以使用“top”或“ps aux|grep sleep”看到它。
But I'm having trouble doing it remotely via ssh. I tried something like
但是我无法通过 ssh 远程执行此操作。我试过类似的东西
ssh remote_machine 'nohup bash -c "( ( sleep 30 ) & )" > out 2> err < /dev/null'
but I cannot figure out where to place the "echo $!" so that it is displayed in my local shell. It is always showing me wrong PIDs, for example the one of the "bash" instance etc.
但我不知道在哪里放置“echo $!” 以便它显示在我的本地 shell 中。它总是向我显示错误的 PID,例如“bash”实例之一等。
Has somebody an idea how to solve this?
有人知道如何解决这个问题吗?
EDIT: OK, the "bash -c" might not be needed here. Like Lotharyx pointed out, I get the right PID just fine using
编辑:好的,这里可能不需要“bash -c”。就像 Lotharyx 指出的那样,我使用正确的 PID 就好了
ssh remote 'nohup sleep 30 > out 2> err < /dev/null & echo $!'
but then the problem is that if you substitute "sleep 30" with something that produces output, say, "echo Hello World!", that output does not end up in the file "out", neither on the local nor on remote side. Anybody got an idea why?
但问题是,如果您将“sleep 30”替换为产生输出的内容,例如“echo Hello World!”,则该输出不会在文件“out”中结束,无论是在本地还是远程端。有人知道为什么吗?
EDIT2: My fault! There was just no space left on the other device, that's why the files "out" and "err" stayed empty!
EDIT2:我的错!另一台设备上没有剩余空间,这就是为什么文件“out”和“err”保持为空!
So this is working. In addition, if one wants to call multiple commands in a row, separated by a semicolon (;), one can still use "bash -c", like so:
所以这是有效的。另外,如果要连续调用多个命令,用分号(;)分隔,仍然可以使用“bash -c”,如下所示:
ssh remote 'nohup bash -c "echo bla;sleep 30;echo blupp" > out 2> err < /dev/null & echo $!'
Then it prints out the PID of the "bash -c" on the local side, which is just fine. (It is impossible to get the PID of the "innermost" or "busy" process, because every program itself can spawn new subprocesses, there is no way to find out...)
然后在本地打印出“bash -c”的PID,就好了。(不可能得到“最里面”或“忙碌”进程的PID,因为每个程序本身都可以产生新的子进程,没有办法找出......)
回答by Brian A. Henning
I tried the following (the local machine is Debian; the remote machine is CentOS), and it worked exactly as I think you're expecting:
我尝试了以下操作(本地机器是 Debian;远程机器是 CentOS),它的工作完全符合我的预期:
~# ssh someone@somewhere 'nohup sleep 30 > out 2> err < /dev/null & echo $!'
someone@somewhere's password:
14193
~#
On the remote machine, I did ps -e, and saw this line:
在远程机器上,我做了 ps -e,看到了这一行:
14193 ? 00:00:00 sleep
So, clearly, on my local machine, the output is the PID of "sleep" executing on the remote machine.
所以,很明显,在我的本地机器上,输出是在远程机器上执行的“睡眠”的 PID。
Why are you adding bash to your command when sending it across an SSH tunnel?
为什么在通过 SSH 隧道发送命令时将 bash 添加到命令中?

