Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-20-2005, 09:37 PM   #1
Registered: Aug 2001
Posts: 77

Rep: Reputation: 15
bash script suggestions for waiting for ssh -L

I need to write a bash script that checks to see if "ssh -L" (forwarding a local port to my backup server) is running; if it isn't, start it, if it is, run the rsync backup, then kill the tunnel.

There are a few problems I don't know how to overcome, though:

1. ssh -L takes a little while to come up sometimes, so the backup has to check the status of the ssh -L command before running, of course.

2. I can't run the ssh -L command more than once, because if I do, it creates multiple tunnels.

3. The ssh -L command doesn't let go of the shell, so I have to stick in a &, which seems like a kludge to me.

I'm a beginner in shell scripting, so my current script is pretty sad, and certain parts don't work yet. Here it is (don't laugh too hard):


if [ -z $(ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}') ]
echo "ssh tunnel is down..."
echo "Bringing up ssh tunnel..."
/usr/bin/ssh -L 873:X.X.X.X:873 -l admin -N X.X.X.X &
echo "ssh tunnel is up"

until [ -n $(ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}') ]
echo "waiting for tunnel..."

if [ -n $(ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}') ]
echo "Starting backup..."
/usr/local/bin/rsync blah blah blah...

if [ -z $(ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}') ]
echo "The tunnel may have disconnected during backup.
kill `ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}'`
echo "Backup completed."

So, first of all, ny "until" command doesn't seem to work at all.
Second, I'm obviously calling for the same info (the pid) many times, and I assume there is a simpler way.

Any suggestions on what would be simpler and better?

Old 03-21-2005, 12:19 AM   #2
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
I have no idea of ssh/rsync (I hope rsync/ssh do return proper return codes), but still...:

sshcmd='ssh -L 873:X.X.X.X:873 -l admin -N X.X.X.X'
backupcmd='/usr/local/bin/rsync blah blah blah'
grepstring='ssh -L'

        ## Kill -0 would return true if the said pid exists
        ## else the process has died or was killed
        if kill -0 $progpid 2>/dev/null
                echo "Program running in background : Pid = $progpid"
                return 0
                echo "Problem : Program could not be started"
                return 1

## Save pid of any process whose command line matches "ssh -L"
sshpid=$(ps -elf | grep "${grepstring}" | grep -v grep | awk '{print $4}')

## If sshpid is null then there is no process matching "ssh -L"
## Else we have the pid of the process.
if [ "${sshpid}x" = "x" ] ; then
        echo "Bringing up : $sshcmd"
        $sshcmd &

        check_pid "${sshpid}"
        [ $? -ne 0 ] && exit 2
        echo "Already up : $sshcmd [Pid : $sshpid]"

## If we reach this stage, we have the ssh tunnel ready
## so we can proceed
echo "Starting backup : $backupcmd"
$backupcmd &             # Fire the backup process in background
bkpid=$!                     # This gives the pid of the background backup process

# Check whether the pid returned still exists
# This is becuase even if you fire an invalid command in background
# you would return the pid BUT it would be gone since ... well, it was invalid!
# So you need check once more.
check_pid "${bkpid}"    
[ $? -ne 0 ] && exit 2   # If not then no point continuing.

# IF we reach this stage, backup process was successfully running in background
echo "Waiting for backup to get over"
wait $bkpid                 # wait command waits for the completion of the given pid
backupstatus=$?         # and $? would contain the completion status of the comand

        if [ $backupstatus -eq 0 ] ; then
                echo "Backup completed successfully"
                echo "Backup may have some problem : return code = $backupstatus"

echo "Cleaning up ssh tunnel"
kill -TERM $sshpid
You can modify this to suit your needs. If you have any doubts (or you spot some goofup!!!), please let me know.


Edit : I have corrected the check_pid function and added some comments. Glad to be of help.

Last edited by dustu76; 03-21-2005 at 10:26 PM.
Old 03-21-2005, 11:22 AM   #3
Registered: Aug 2001
Posts: 77

Original Poster
Rep: Reputation: 15
Wow. Thanks very much. You didn't need to write the whole thing for me .

You're a little over my head, though, with some of this script. If you have the time, could you run some basic comments about what this is doing?

If not, that's ok. I will do some more reading and try to break it down.


Old 03-22-2005, 07:59 AM   #4
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33

Without having even glanced at the script posted by dustu76, I'll offer you a link instead. Check out Sub Shells and more importantly, look at example 20-3 at the bottom of the page. It shows an example of executing commands into it's own process, and then having the master script wait for the subs to finish. I think that this would serve your purposes nicely. Plus, it's a chance to learn something new. (=

Happy bashing!
Old 03-22-2005, 07:04 PM   #5
Registered: Aug 2001
Posts: 77

Original Poster
Rep: Reputation: 15
Thanks for the link. I'll check it out as soon as I can. I have read portions of the Advanced Bash-Scripting Guide - I guess I should read the whole thing.

Thanks again to you and dustu76. I think I can take it from here (and I've already learned a few things!).



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard Drive Problems: timeout waiting for DMA; error waiting for DMA mintee Linux - Hardware 10 09-21-2007 05:06 AM
Selectively 'waiting' in a shell script subu_s Programming 2 12-20-2004 05:57 AM
waiting script Sn7 Linux - Software 2 09-20-2004 12:50 PM
Bash Scripting - Waiting for a certain amount of data welby Programming 12 05-12-2004 12:40 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:41 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration