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): #!/bin/bash if [ -z $(ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}') ] then 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 & else echo "ssh tunnel is up" fi until [ -n $(ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}') ] do echo "waiting for tunnel..." done if [ -n $(ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}') ] then echo "Starting backup..." /usr/local/bin/rsync blah blah blah... fi if [ -z $(ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}') ] then echo "The tunnel may have disconnected during backup. else kill `ps -elf | grep 'ssh -L' | grep -v grep | awk '{print $4}'` echo "Backup completed." fi exit 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? Bret |
I have no idea of ssh/rsync (I hope rsync/ssh do return proper return codes), but still...:
Code:
#!/bin/bash HTH Edit : I have corrected the check_pid function and added some comments. Glad to be of help. |
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. Thanks! Bret |
iggymac:
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! |
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!). Bret |
All times are GMT -5. The time now is 01:14 PM. |