jaeyong
jaeyong

Reputation: 9363

About the internals of nohup ssh

I mindlessly use nohup ssh for issuing a remote ssh command without worrying about the accident hangup. Now I'm starting to think about it and it is not pretty clear.

What I'm wondering is that why just doing "ssh remote sleep 100 &" stops the job after few seconds? For instance,

$ ssh remote sleep 100 &
[1] 13358
$ 
[1]+  Stopped                 ssh remote sleep 100

By what reason is this job stopped? Could you explain the internals of this job control?

Upvotes: 1

Views: 247

Answers (2)

chepner
chepner

Reputation: 531175

The command

ssh remote sleep 100 &

only runs ssh in the background. Once ssh is started on the local machine, control returns to the local shell, regardless of what is running (via sshd) on the remote end.

Upvotes: 0

Olivier Dulac
Olivier Dulac

Reputation: 3791

  • If you want the remote command to keep working until it's finished (and not depend on the ssh connection with the remote host): you could use screen (or tmux) .

    • connect using ssh to the remote host
    • once connected: screen to start a screen session (a kind of "virtual terminal", that will keep running until you close it, instead of depending on your own connection to it)
    • you can then detach from screen (ctrl-a d) and re-attach to it later (from another machine, etc) : just ssh again, "screen -l" to list screens, and "screen -r" to re-attach to one. Read about screen on the net.
  • The reason your job is stopped is not linked to the command, but to the internal of job handling. Some (good) infos can be found on http://www.linusakesson.net/programming/tty/ (search for background if you don't read the whole thing. But read the whole thing ^^). In a nutshell ... writing to a TTY from a background job will cause a SIGTTOU to suspend the entire process group (maybe your ssh asked for a password? or it displays something when connecting?)

The advantage of screen over running on the remote host usign "nohup" are numerous. The main one is that if you try to re-connect to a nohup program (ex: vi) it can't (easily) be done... especially if it is multi-line. But when you re-attach to a screen session, you see the (virtual) terminal as if you never left it (ie, it's updated if the command added things on the screen, and it still have rows/columns, etc).

You can also work at several person on the same terminal (or have some person "view it" while one works in it).

Etc.

Upvotes: 1

Related Questions