1830060 Members
2764 Online
109998 Solutions
New Discussion

ssh bash shell hangs...

 
SOLVED
Go to solution
TJ_16
Frequent Advisor

ssh bash shell hangs...

with the large number of linux servers I admin, I have set up one admin host with private keys to ssh to all my other hosts. I find that when I ssh to a host or set up a for loop script to ssh a command on all my hosts, and run a command (like /etc/init.d/snmpd restart), then Ctrl-D to exit back to my admin host, my ssh hangs.
I believe I found sort of a fix for this-
run the commands with this syntax:
some-command /dev/null 2>&1 &

Was just wondering if there was some ssh setting or some other way to prevent these hangs?

Thanks,
TJ
5 REPLIES 5
Claudio Cilloni
Honored Contributor

Re: ssh bash shell hangs...

I made the same thing and... it works for me.
You can try adding nohup command:

# nohup some-command /dev/null 2>&1 &

maybe this helps.

Ciao
Aco Blazeski
Regular Advisor

Re: ssh bash shell hangs...

Hi folks,

can you explain what is the purpose of redirecting input "
Thanks
Matti_Kurkela
Honored Contributor
Solution

Re: ssh bash shell hangs...

The SSH connection is shut down by the sshd (at the destination host) when the following conditions are true:
- the process sshd has started (usually some sort of a shell) finishes
- no process is holding onto the standard input, output and error streams opened for the SSH session
- all X11 or TCP port forwarding connections are shut down, if any are used

When you want to use SSH to "launch" something on some remote host and leave it running there on its own, you must explicitly disconnect the launched process from the standard input, output and error streams so that sshd won't hold the connection open.

You *can* force the connection to shut down from the local side, but you must use SSH's escape characters for that. By default the key combination to disconnect is "~."

The "
The ">/dev/null 2>&1" is a shorthand for ">/dev/null 2>/dev/null" which redirects both the standard output (">") and standard error ("2>") streams to /dev/null aka the bit-bucket.

Depending on the features of the shell you're using, the final "&" may or may not be enough to leave the command running after the SSH connection closes. Some (most?) shells send a SIGHUP to any processes of the session left running when the session ends. To counteract that, you need to prefix your command with "nohup"... or detach the process from the session with "detach" if your shell supports that command.
MK
Aco Blazeski
Regular Advisor

Re: ssh bash shell hangs...

10x a lot Matti for detailed explanation !

I expect TJ to assign some points in my name (since I'm not the author of the thread).

Thanks to all
TJ_16
Frequent Advisor

Re: ssh bash shell hangs...

points assigned and thanks for the responses.