I'm trying to open a tunnel on a remote server from local machine by executing a command remotely via ssh. I'm using sshpass too.

sshpass -fpw_file.txt ssh user@host.com 'bash /home/username/do_stuff.sh &'

This do_stuff.sh contains just one line:

ssh -f -N -L 8888:hostname:9999 user@host &

But the call (first command) is not returning. If I put & outside the single quotes, it spawns a process which is not getting killed. Although it is a very small memory leak, it would be good to know if there is a way to do this cleanly.


  • Have you tried redirecting the i/o streams to null? sshpass -fpw_file.txt ssh user@host.com 'bash /home/username/do_stuff.sh &> /dev/null < /dev/null &' ssh won't exit until all streams are closed. – Zachary Brady Oct 20 '16 at 17:08
  • That worked for me! Thanks! I would love to know details of the last part. I understand that we're redirecting output of 'bash /path/to/file.sh' to /dev/null. But what is '< /dev/null &' doing? – Nirmal Oct 20 '16 at 17:36

Redirect the i/o streams to null. BG processes with open descriptors can cause SSH to not close.

sshpass -fpw_file.txt ssh user@host.com 'bash /home/username/do_stuff.sh &> /dev/null < /dev/null &'


&> /dev/null takes stdout and stderr and redirects it to null

< /dev/null takes stdin and redirects it to null

& at the end sends the process to the background.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.