Why does SSH hang at the end of these commands and how can I make it exit?

10

4

I run this:

ssh -t -vvv -i ~/.ssh/druid-keypair -o StrictHostKeyChecking=no ubuntu@${INSTANCE_ADDRESS} <<EOI

# Setup Oracle Java
...

# Install dependencies - mysql must be built from source, as the 12.04 apt-get hangs
export DEBIAN_FRONTEND=noninteractive
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password diurd'
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password diurd'
sudo apt-get -q -y -V --force-yes --reinstall install mysql-server-5.5

echo "ALL DONE with druid environment setup!"
exit
EOI

Note: I have tried with and without -t in ssh.

The debug output from -vvv is this:

...
ldconfig deferred processing now taking place
ALL DONE with druid environment setup!
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0

And then the process just sits there forever. Why won't the ssh command end? I have tried with -t and without, and I have tried with the exit and without. It doesn't make a difference :(

Update: When I type 'jobs' at the end of the script, I see:

JOBS:
[1]-  Running                 nohup bin/zookeeper-server-start.sh config/zookeeper.properties 2>&1 > /dev/null &
[2]+  Running                 nohup bin/kafka-server-start.sh config/server.properties 2>&1 > /dev/null &

How can I run these services and still have an ssh session that ends?

Update: I now manually disown these processes. The thing still doesn't exit. WTF mate?

Update: When executing line by line, two commands don't return to shell without hitting CR:

nohup bin/zookeeper-server-start.sh config/zookeeper.properties &
nohup bin/kafka-server-start.sh config/server.properties &

rjurney

Posted 2013-07-08T23:52:13.380

Reputation: 203

Try to use -q (quiet mode) instead of -vvv (verbose mode). – september – 2013-07-09T00:42:17.877

no effect. -vvv was just to debug. – rjurney – 2013-07-09T03:22:59.717

SSH to server and execute all these commands one by one to figure out hang reason. – september – 2013-07-09T04:19:55.497

1Thats the thing - none of them hang! This works fine in console. – rjurney – 2013-07-09T04:44:49.283

Does it work if you replace the contents of the <<EOI block with something like "echo 1"? – ed. – 2013-07-09T21:08:46.163

Yes. Two commands hang until you hit CR:

nohup bin/zookeeper-server-start.sh config/zookeeper.properties & nohup bin/kafka-server-start.sh config/server.properties & – rjurney – 2013-07-09T21:25:04.667

I redirect output of these two commands to >/dev/null and they no longer hang until CR. But the ssh still hangs :( – rjurney – 2013-07-10T01:25:25.667

Answers

22

Typically, SSH terminal sessions hang if there are still background connections still open. By background connections, I mean things such as:

  • X11 window forwarding
  • STDOUT and STDERR

Have a look at the connections that are still active on your hung SSH session by typing ~# in your hung SSH terminal.

It could be that your script is opening sessions that you didn't realize. Or your remote machine's terminal configs like .profile (or .bashrc, etc.) may have something in it that establishes a session. Good luck hunting!

By the way, some of the other escape sequences offered by OpenSSH clients may also be useful:

Supported escape sequences:
  ~.  - terminate connection (and any multiplexed sessions)
  ~B  - send a BREAK to the remote system
  ~C  - open a command line
  ~R  - Request rekey (SSH protocol 2 only)
  ~^Z - suspend ssh
  ~#  - list forwarded connections
  ~&  - background ssh (when waiting for connections to terminate)
  ~?  - this message
  ~~  - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)

One other thing, if you want your SSH to just run your commands and immediately exit -- that is, you don't want a remote terminal session -- you can use the -f option to ssh. That will force the SSH connection to be a background job.

padub

Posted 2013-07-08T23:52:13.380

Reputation: 361

3The following connections are open: #0 client-session (t4 r0 i0/0 o0/0 fd 5/6 cc -1)

This is what I get. Does this mean anything? – moleculezz – 2015-01-16T21:40:29.623

-f works for me. – Jingguo Yao – 2017-08-13T18:49:03.127

I heard of -n for the same purpose but that didnt work for me! – Kostas – 2019-04-20T21:05:15.787

Does a background process count as a background connection and can one disown the background process so the ssh connection doesn't hang? – the_prole – 2019-09-09T23:18:46.973