获取进程的PID通过ssh开始的nohup [英] Get the PID of a process started with nohup via ssh

查看:1599
本文介绍了获取进程的PID通过ssh开始的nohup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始通过ssh使用nohup的远程计算机上的进程。现在的问题是如何获得的PID过程始于nohup的,所以过程,实际上做的事情,而不是一些外壳实例等。另外,我想存储在文件输出和错误,但在这里不是问题...

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...

在当地,它完美的作品使用

Locally, it works flawlessly using

nohup sleep 30 > out 2> err < /dev/null & echo $!

据呼应我命令睡眠30的具体PID,这也是我可以看到使用顶或的ps aux | grep的睡眠。

It is echoing me the exact PID of the command "sleep 30", which I can also see using "top" or "ps aux|grep sleep".

但我无法通过SSH远程做。我想是这样

But I'm having trouble doing it remotely via ssh. I tried something like

ssh remote_machine 'nohup bash -c "( ( sleep 30 ) & )" > out 2> err < /dev/null'

但我想不通的地方放置回声$!所以,它显示在我的本地shell。它总是向我展示错的PID,例如庆典实例等的之一。

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.

有人偷一个想法如何解决这个问题?

Has somebody an idea how to solve this?

编辑:
OK,可能不会在这里需要的bash -c。像Lotharyx指出,我得到正确的PID就好用

OK, the "bash -c" might not be needed here. Like Lotharyx pointed out, I get the right PID just fine using

ssh remote 'nohup sleep 30 > out 2> err < /dev/null & echo $!'

但随后的问题是,如果你代替休眠30的东西,产生输出,也就是说,呼应的Hello World!,则输出不会在文件中结束了去,既不在当地也没有对远程端。任何人有一个想法,为什么?

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?

EDIT2:我的错!有,只是没有留下其他设备上的空间,这就是为什么文件out和犯错一直都是空的!

My fault! There was just no space left on the other device, that's why the files "out" and "err" stayed empty!

因此​​,这是工作。此外,如果要调用多个命令一排,用分号(;)隔开,人们仍然可以使用庆典-c,像这样:

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:

ssh remote 'nohup bash -c "echo bla;sleep 30;echo blupp" > out 2> err < /dev/null & echo $!'

然后打印出的庆典-c的PID在本地端,这是蛮好的。 (这是不可能获得最里面的或忙​​进程的PID,因为每个程序本身可以生成新的子进程,有没有办法,找出...)

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...)

推荐答案

我尝试以下(本地计算机是Debian的;远程机器是CentOS的),和它的工作正是我想你期待:

I tried the following (the local machine is Debian; the remote machine is CentOS), and it worked exactly as I think you're expecting:


    ~# ssh someone@somewhere 'nohup sleep 30 > out 2> err < /dev/null & echo $!'
    someone@somewhere's password:
    14193
    ~#

在远程计算机上,我没有PS -e,看到这一行:

On the remote machine, I did ps -e, and saw this line:


    14193 ?        00:00:00 sleep

所以,很显然,我的本地机器上,输出的是睡在远程机器上执行。

So, clearly, on my local machine, the output is the PID of "sleep" executing on the remote machine.

为什么要跨一个SSH隧道发送时,你的bash添加到您的命令?

Why are you adding bash to your command when sending it across an SSH tunnel?

这篇关于获取进程的PID通过ssh开始的nohup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆