Running function or command concurrently in a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running function or command concurrently in a bash script
# 1  
Old 01-19-2009
CPU & Memory Running function or command concurrently in a bash script

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.....

Code:
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

# 2  
Old 02-03-2009
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:
Code:
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


Last edited by otheus; 02-04-2009 at 10:22 AM.. Reason: space between $$ >
# 3  
Old 02-04-2009
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.
# 4  
Old 02-04-2009
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.
# 5  
Old 02-04-2009
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:

Code:
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)?

Sorry in advance for my ignorance.....

Last edited by otheus; 02-04-2009 at 10:22 AM.. Reason: space between $$ >
# 6  
Old 02-04-2009
The file appears at execution of the 2nd line of the script, namely:
Code:
echo $$ >LOCK #Puts the pid into a file called LOCK

Also, note: you have to fill out the lines only in comments -- starting the tunnel and testing to see if it's still up.

Last edited by otheus; 02-04-2009 at 10:21 AM.. Reason: should be a space before the >
# 7  
Old 02-05-2009
Bug

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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need a Bash script for iterating thru an array and running a command

Hi , I am a total beginner so bear with me. I have the below code which works . I need to extend it by iterating thru the array arr and executing a command in each loop. some thing on the lines of below. I need to run this in a Jenkins script , so I would need below bash script to run... (6 Replies)
Discussion started by: SVRao19056
6 Replies

2. Shell Programming and Scripting

[bash] running a function remotely using ssh

Hi all. I need a bash script to run a function remotely. I think it should be similar to the following but can't figure out the exact syntax. Here is the script: #!/bin/bash function nu () { for var in {0..5}; do printf "$var, "; done; echo } ssh host "$(typeset -f); nu" ... (9 Replies)
Discussion started by: ziguy
9 Replies

3. Shell Programming and Scripting

Command not found, but using function in bash

In the bash below, if the answer is "y" then goto function remove. If the answer is "n" then goto the id variable line (where the date is inputted). However, I am getting command remove not found, but remove is a function not an command. I must have the syntax incorrect? Thank you :). ... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Script function which checks if itself is already running

Hi All, I have a cron job set up which is set to run every 10 seconds. What I need to do is have the script do a check to see if it is already running such that if it is running it wont fire up additional instances and processes according to its normal process. For example if I have a script... (4 Replies)
Discussion started by: landossa
4 Replies

5. Shell Programming and Scripting

Function not running in script.

I an using the below functions in my script. --------- checkRT() { subHeader "Runtime Check" for nn in `cat $HOST_FILE|egrep -i 'msdp|ca|backup|db'|grep -v '#'|sed '/^$/d'|awk '{ print $1":"$2":"$3}'` do fhba=`expr $nn|cut -d: -f2` fhba2=`expr $nn|cut -d: -f3` subSubHeader $fhba2 ssh... (1 Reply)
Discussion started by: nandan8a
1 Replies

6. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

7. UNIX for Dummies Questions & Answers

How to run script concurrently

Hi all, I have one script. Its job is to get 1 file from dirA and 1 file from dirB as parameters for another python script. However, there are a lot of files in both dir A and B, so running this scripts really takes a lot of time. So I wonder if there are any ways that I can do this faster,... (6 Replies)
Discussion started by: yoyomano
6 Replies

8. Shell Programming and Scripting

Running a function on a remote server via SSH in a script

I'm working on a script (mostly for practice) to simplify a task I have to do every now and then. I have a cluster with 6 servers on it, each server has a directory with a set of files called *.pid and *.mpid. Each file contains the pid of a process that may or may not be running on that server.... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

9. UNIX for Dummies Questions & Answers

Alias, function or script (bash) to "revert" cd command?

In all of my brief and superficial experience with Unix or Linux, the one curious and consistent thing has been that 'cd ./' (back up one directory level) has done absolutely nothing in any of them. Now I understand that, at least for bash, 'cd ./' appears to have been substituted by 'cd ..' Am... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

10. Shell Programming and Scripting

Running same script multiple times concurrently...

Hi, I was hoping someone would be able to help me out. I've got a Python script that I need to run 60 times concurrently (with the number added as an argument each time) via nightly cron. I figured that this would work: 30 1 * * * for i in $(seq 0 59); do $i \&; done However, it seems to... (4 Replies)
Discussion started by: ckhowe
4 Replies
Login or Register to Ask a Question