Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am running a GUI app (Gnucash) that connects to a remote Postgres database via a secure shell session. I can use the SSH -L command to tunnel a local port and then separately run Gnucash and this works fine.

What I'd like to do is use a single shell script that sets up the tunnel and then calls Gnucash. Is that possible? If so, how do I do it? Currently, I run commands like the following in 2 separate terminal windows:

ssh -L 5433: gnucash postgres://gnucash@localhost:5433/gnucash_db

If I simply put both lines in a shell script, the first line drops me into the remote shell and the second line doesn't execute until I exit the remote shell.

TIA, Rob Hills

share|improve this question
How about adding a & to the end of the first line? Would that help? – Ian B. Aug 30 '12 at 0:48
I've not yet tried that, partly because I've been distracted by another task and partly because of the belief that using the & at the end would leave the tunnel process in the background, leaving the connection open. Not sure if it would close when I finished the script or if it would stay open until I shut down Ubuntu. I'll have to test this when I get a chance. – Rob Hills Aug 30 '12 at 5:58
up vote 0 down vote accepted

Use ssh -fNT -L 5433: to ask ssh to automatically background after establishing the tunnel, and then your script can move on the next command. I suggest you end the script with an exit, which will close the terminal and kill the ssh process; otherwise you must either explicitly kill it or it will exit if/when the server times it out.

share|improve this answer
He wants to run gnucash on the remote server, so presumably he doesn't want -N. Not sure how the -T interacts either, but the -f should do the trick. – Steve Kroon Aug 31 '12 at 8:21
No, Gnucash is running locally, using a remote postgres database. The answer by izx works perfectly for me, many thanks. – Rob Hills Sep 1 '12 at 8:08
This does not kill the ssh process, even with the explicit exit (which has no effect, since the script is would exit at the end of code anyway). – Matthew Flaschen Dec 9 '12 at 5:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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