Reply to comment

Remote command through ssh hangs?

One day I found a script hangs in the middle when it was executed as a remote command through ssh. I log on to that node and run the same script without any problem.  There was nothing special except a background command in the script. Let's just look at this script:


sleep 20 &
echo done

You get message "done" and the shell immediately after issue the command directly on the node, but it hangs for 20 seconds if submiited as "ssh localhost".

However, this script


sleep 20 & > /dev/null
echo done

Runs perfectly fine on local or through ssh.


A bit of research reveals sshd is responsible for this behavior. To put it simple, sshd doesn't close when the script finishes, instead, it closes when all related I/O sees EOF. This design is to prevent premature close of ssh connection (race condition) between a script and its commands.





  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options