Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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:127.0.0.1:19097 username@myserver.com.au 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
2  
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
add comment

1 Answer

up vote 0 down vote accepted

Use ssh -fNT -L 5433:127.0.0.1:19097 username@myserver.com.au 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
add comment

Your Answer

 
discard

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.