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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command Timeout

Status
Not open for further replies.

TamedTech

IS-IT--Management
May 3, 2005
998
GB
Hello guys,

I have a command that runs and occasionaly sits with no response for the client machine, which messes up my entire loop.

So I need to set a timeout on the command so that it only lasts for say 30 seconds before progressing onto the next instruction.

I know that my command doesn't have a Timeout flag for it, so I could use an alternative method.

Thanks,

Rob
 
Here's a working example of exactly that
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 -A arr $(jobs -p) 
  [[ ${#arr[*]} -gt 0 ]] || break;
  [[ $TOT_WAIT -gt $MAX_WAIT ]] && { kill ${arr[*]}; break; }
  sleep $SHORT_WAIT
  (( TOT_WAIT += $SHORT_WAIT ))
done;

Sorry it's not very well commented. The key is to run the jobs in the background and watch their progress with 'jobs -p'

Ceci n'est pas une signature
Columb Healy
 
Hi

Nice script, Columb. Could you tell us, please, for what shell was written/used ? Although I would say that works with both [tt]bash[/tt] and [tt]ksh[/tt], I am curious about that [tt][highlight lightcyan]while [red][[/red] : [red]][/red][/highlight][/tt] and [tt][highlight lightcyan][[red][[/red] ${#arr[*]} -gt 0 [red]][/red]][/highlight][/tt]. Because the characters in red are needless in [tt]bash[/tt] and as far as I know in [tt]ksh[/tt] too.

Thanks.

Feherke.
 
Feherke, I agree with you about the brackets being needless for [ : ] (my personal habit is to use while true in this scenario), however if it is a ksh script then [[ and ]] are not equivalent to [ and ]. [ would be a reference to /usr/bin/test whereas [[ would use ksh's built-in conditional operator, which has different functionality.

Annihilannic.
 
Hello Guys,

Thanks for getting back to me on this one, for the record i'm using Bash Shell as far as i know, i'm quite new to all this but i know when i place in dud commands i get a Bash: not recognised type message.

Is there any chance you guys can explain the code snippet above in a little more details by commenting it perhaps, just to help me understand the concept.

It seems like it will achieve what i want it too.

Thanks,

Rob
 
In pseudo-code:

[tt]while true do (i.e. loop forever)
set arr array values to the list of jobs running under this shell
if no more jobs are running, break out of the while loop
if we have waited longer than MAX_WAIT, kill the jobs
in array arr and break out of the while loop
sleep for SHORT_WAIT seconds
add the value of SHORT_WAIT to the TOTAL_WAIT
end while loop[/tt]

I didn't bother to describe the first parts because they are specific to columb's own job...i.e. you would replace those commands with your own long-running job.


Annihilannic.
 
Thanks pal,

That makes it alot clearer, i'm in the office tommorow so i'll have a tinker around with the code and see what i can achieve from it.

Rob
 
Hi

Annihilannic said:
however if it is a ksh script then [[ and ]] are not equivalent to [ and ]. [ would be a reference to /usr/bin/test whereas [[ would use ksh's built-in conditional operator, which has different functionality.
So you say that the double brackets ( [] ) are used not because the -gt operator needs them, just to force the use of internal evaluator instead of an external command. Thanks Annihilannic, I did not thought to this.

However I sustain my previous statement and I consider it useless. Both in [tt]ksh[/tt] and [tt]bash[/tt] know about simple bracket too.
Code:
[gray]# KSH[/gray]
[blue]master #[/blue] echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2
[blue]master #[/blue] type [
[ is a shell builtin

[gray]# BASH[/gray]
[blue]master #[/blue] echo $BASH_VERSION
2.05b.0(1)-release
[blue]master #[/blue] type [
[ is a shell builtin

Feherke.
 
FYI

I'm using ksh on an AIX 5.1 OS.

Why do I use the square brackets - because I'm not as clever as you! Seriously, I use [[ condition ]] because it's what I know, and it's more concise than 'test'

If anyone would like to improve my code, please feel free, I'm not proud and this is the place to exchange ideas

Ceci n'est pas une signature
Columb Healy
 
True, true feherke... I'm not sure where I got the idea that it wasn't a built-in. Even in sh. So why does /usr/bin/[ exist then I wonder, so you can use it from csh and the like?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top