Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sleep & re-run script until condition is met 1

Status
Not open for further replies.

blodwyn

IS-IT--Management
Mar 23, 2001
24
0
0
Hi

I have a basic script that works OK, but I need to adjust it. At the moment, if a certain process is running at the time, it will abort.

I need to enhance it so that, in that situation, it will sleep for a designated period before retrying the step, and continue to do so until the process has ended and it can carry on with the rest of the script.

I'm sure it's a simple answer - but not to me at the moment!

Thanks

Blodwyn
 
Hi

Code:
#!/bin/sh

while [ ! [i][green]condition is met[/green][/i] ]; do
  [i][green]do so[/green][/i]
  sleep [i][green]for a designated period[/green][/i]
done

[i][green]carry on with the rest of the script.[/green][/i]
Of course, if you give us more details, probably we can propose a more adequate solution.

Feherke.
 
This is a real life extract from one of my scripts which gives an example of feherke's code.
The situaton is that I do a tidy up using 'find', sometimes this hangs (I don't know why) so I want to either wait untill they're finnished or five minutes.
Code:
# Tidy up any lock files left behind
for env in ${envs[*]}
do
 nohup find /staffware/gsc${env}01/queues -name CurrentCase.dat -exec rm {} \; &
 nohup find /staffware/gsc${env}01/cms.rx -name "*.lok" -exec rm {} \; &
 nohup find /staffware/gsc${env}01/cms.rx -name "sleep.*" -exec rm {} \; &
done

# Wait until finds have complete - or 300 secs whichever is shorter
TOT_WAIT=0
SHORT_WAIT=10
MAX_WAIT=300
while [ : ]
do
  # Set an array of job numbers
  set -A arr $(jobs -p)
  # one or more jobs still running or break
  [[ ${#arr[*]} -gt 0 ]] || break;
  # If beyond MAX_WAIT then kill of remaining jobs and break
  [[ $TOT_WAIT -gt $MAX_WAIT ]] && { kill ${arr[*]}; break; }
  # wait for SHORT_WAIT seconds
  sleep $SHORT_WAIT
  # update TOT_WAIT
  (( TOT_WAIT += $SHORT_WAIT ))
done;

Ceci n'est pas un signature
Columb Healy
 
That's great, thanks.

What if I wanted to limit the number of re-tries to, say 5, so that after 5 attempts it no longer sleeps but aborts?

Cheers

Blodwyn
 
Hi

Blodwyn said:
What if I wanted to limit the number of re-tries to, say 5, so that after 5 attempts it no longer sleeps but aborts?
Then you have to include a counter, something like this :
Code:
count=0
while [ ! [green][i]condition is met[/i][/green] -a $count -lt 5 ]; do
  [green][i]do so[/i][/green]
  ((count++))
  sleep [green][i]for a designated period[/i][/green]
done

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top