Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the PID of a process started with nohup via ssh

Tags:

bash

ssh

nohup

pid

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

like image 452
proggy Avatar asked Sep 28 '12 21:09

proggy


People also ask

How do I get PID of nohup?

When using nohup and you put the task in the background, the background operator ( & ) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via kill PID or kill -9 PID (if you need to force kill).

How do I see the nohup process?

Using the lsof Command Here, the lsof command lists the PID and name of the processes that are writing to the nohup. out file. As expected, our sleep process with a PID of 1138 appears in the output. As we can observe, the lsof command gave an error since the output of the nohup command is being redirected to output.

How do I find the process ID in Linux?

A process is nothing but running instance of a program and each process has a unique PID on a Unix-like system. The easiest way to find out if process is running is run ps aux command and grep process name.

What is SSH nohup?

Nohup stands for 'no hang up'. It's an extremely useful command to make your process running even after you log out. One of the most common use can be found in running time-taking commands over SSH connection.


1 Answers

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?

like image 162
Brian A. Henning Avatar answered Oct 22 '22 13:10

Brian A. Henning