I currently run a script over a vpnc tunnel to back-up my data to a remote server. However for a number of reasons the tunnel often collapses. If I manually restore the tunnel then the whole thing can continue, but I want to add into my script a section whereby while the transfer is taking place, the system checks if the tunnel is still up and if not it resets it. I though I had it cracked (see below), but even though the $BREAK variablet change after the process code had run, it didn't escape the while loop so the script never ended. I therefore think I need a different way of doing it (by the way I am new to scripting so expect some ugly code).....
I am not asking someone to write this for me, but if you know of a function that I could use that would be most helpful..... Thanks in advance.....
function process ()
{
process code.....
BREAK=500
}
# ------------- the script itself --------------------------------------
BREAK=0
# First run Tunnel to get it up and running (a separate script).
Tunnel.sh
# Run process and the while loop at the same time.
process &
while [ $BREAK == 0 ]
do
Tunnel.sh #Re-establishes connection if required
sleep 15
done
An external process cannot change a variable in your current process. So BREAK cannot get changed from anything in process or Tunnel.sh.
However, you can switch on the existence of a file, like this:
LOCK=/var/run/tunnel-keepalive.pid
echo $$ >LOCK
# start tunnel here
while [ -f $LOCK ]; do
# Test to see if tunnel is still up, if not: start again
sleep 5
done
otheus thanks for getting back to me and explaining that one can't change a variable within a process. I see that generating a file could get round this problem, however I am afraid that I don't really understand what the script does. I have tried to use it and can see that it generates a file called LOCK which contains the process id. However I can't see how the file is removed if the process dies.
I also can't see the tunnel-keepalive.pid appear in var/run - should I?
Sorry if these are really ignorant questions. If you could point me in the right direction / webpage I would be really grateful.
The file isn't removed if the process dies. I thought the idea was to have your internet connection always stay up, until you want to take it down manually somehow. If it drops, but not by your desire, this script tries to keep it up. When you want to shut it down, you have a script that (a) removes the LOCK file and (b) kills the tunnel.
Otheus thanks for the rapid response. Yes you are right - I just want the script to constantly monitor the connection and start it again if it stops.
So if I run the script that you gave me it looks for the tunnel-keepalive.pid in /var/run, but nothing by this name appears there. So the script will never enter the while loop. The script doesn't seem to run based on the existence of a file in the local directory. I can only get it to run if I do this:
LOCK=/var/run/tunnel-keepalive.pid
echo $$ >LOCK #Puts the pid into a file called LOCK
LOCK=./LOCK
# start tunnel here
while [ -f $LOCK ]; do
# Test to see if tunnel is still up, if not: start again
sleep 5
done
I know that the above works - if I delete the file LOCK then the while loop stops, but I feel like I am missing the point or pulling the pid into a file - or could I just use the presence of any old file to maintain the while loop (and therefore the tunnel)?
Right, I think everything is working well now. I call up the tunnelkeepalive script from within another one, then when that one has finished doing what it needs to do, it deletes the LOCK file thus stopping tunnel maintenance and then disconnects from the vpnc.
otheus thanks for your help - really appreciate it. I never realised that by defining a variable in /var/run/ it would be assigned a pid. While this is a bit off-topic - are the other .pid files in /var/run/ dynamic i.e. if one were to kill the pid in that .pid file that .pid file would disappear?
That's not exactly what's happening, my friend. The $$ expands to the process ID of the currently running script. So your "keepalive" script echo's that out to the file.
Generally no. Those PID files are created by the "init" scripts. You can see these in the scripts in /etc/rc${RUNLEVEL}.d/ where RUNLEVEL is the number you see with /sbin/runlevel. These scripts use the PID files so that they can easily stop and restart the correct service. If you remove the pid file, these scripts can be confused and cause programs not to shutdown properly. If you have a mysql database running, for instance, and you shutdown, if the pid file is no longer there, mysql might be shutdown abruptly before it has a chance to flush its buffers, resulting in data corruption.
Aahhh that makes a lot more sense! I was wondering what the $$ meant - not the easiest thing to search on google by the way to try and find out. Will leave the /var/run/ .pid files well alone!
Thanks for taking the time to explain. I have got a lot more learning to do......