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

Script

Status
Not open for further replies.

Igaduma

Technical User
Nov 23, 2001
322
BE
Hi all,

How could I set up a script that would execute a command, but only for a given period of time.

Say I want to do a ls -R in a deep directory, but only during 20 seconds, and then bail out & stop the script.

Thanks for any help.

Iga-Duma
 
no way to do this by SCRIPT
you need at least PERL, i would do it in C, somethink like:

a) start a timer
b) fork the ls cmd
c) if after ?time? children still alive, kill process
d) exit
 
I do not try it, but this should work in ksh:
Code:
ls -lR > output &
sleep 20
kill %+

In ksh, %+ is the last background job.
But what if the script is done in less than 20 seconds:
the previous background job (if any) is killed.
So, this should be used in a script that launch only one background job.

I think you could also use:
Code:
kill %ls
In this case %ls refers to the last background job whose command starts with 'ls'.
So, this should be used in a script that launch only one background job whose command starts with 'ls'.
 
Perhaps something like:

count=1
while [ count -le 20 ]
do
ls -R
sleep 1
count=`expr $count + 1`
done

OK this won't be exactly 20 seconds, but I assume we're not talking a life or death need for such exactitude are we? HTH.
 
Ah no, that's rubbish from me isn't it? The ls -R would start again each time! Go with dchoulette's suggestions.
 
Hi!
This one works for ksh

(while [ true ];do ls -R <Directory Name>;done) &
sleep 20
kill -9 $!


The kill command kills the batch and all its childs (in this case just one)

Cheers!
Marcos.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top