2

I want to write a bash script that:

  • establishes an SSH tunnel
  • launches a local VNC client (vncviewer, the RealVNC viewer in my case)
  • closes the SSH tunnel once the vncviewer exits

Best effort so far:

ssh -L 5900:localhost:5900 <my IP address> -N &
pid=$!
/Applications/VNC\ Viewer.app/Contents/MacOS/vncviewer localhost:5900
kill "$pid"

I couldn't get it to work with open, though. This works, but the script exits leaving an open tunnel behind:

ssh -L 5900:localhost:5900 <my IP address> -N -f sleep 10
open -a 'VNC Viewer' --args localhost:5900 

Using open seems to be more intuitive and user friendly.

1 Answer 1

2

From man open:

 -W  Causes open to wait until the applications it opens (or that were already open) have exited. 

So unless VNC Viewer is just a launcher, running

ssh -L 5900:localhost:5900 <my IP address> -N &
pid=$!
sleep 5
open -W -a 'VNC Viewer' --args localhost:5900
kill "$pid"

should do the trick.

1
  • 1
    So much of half an hour spent on reading man open :). Thanks a bunch, it works, answer accepted!
    – badbishop
    Commented Apr 21, 2022 at 17:00

You must log in to answer this question.

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