4

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 [email protected] '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.

Thanks!

2
  • Have you tried redirecting the i/o streams to null? sshpass -fpw_file.txt ssh [email protected] 'bash /home/username/do_stuff.sh &> /dev/null < /dev/null &' ssh won't exit until all streams are closed. Commented Oct 20, 2016 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
    Commented Oct 20, 2016 at 17:36

1 Answer 1

5

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

sshpass -fpw_file.txt ssh [email protected] 'bash /home/username/do_stuff.sh &> /dev/null < /dev/null &'

Explanation

&> /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.

1
  • Nice. So many people have asked this and the first real answer.
    – Blaze
    Commented Oct 14, 2020 at 8:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .