Get the PID of a process started with nohup via ssh

proggy picture proggy · Sep 28, 2012 · Viewed 10.7k times · Source

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 $!

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

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'

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?

EDIT: 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 $!'

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: My fault! There was just no space left on the other device, that's why the files "out" and "err" stayed empty!

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 $!'

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

Answer

Brian A. Henning picture Brian A. Henning · Sep 28, 2012

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
    ~#

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.

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