Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop over a certain time?

I am creating a script that should wait until a certain file (e.g. stop.dat) appears or after certain time (e.g. 500 Seconds) has passed.

I know how to wait until the file appears:

while [ ! -f ./stop.dat ]; do
  sleep 30
done

How can I add the other statement in my while loop?

like image 221
daxterss Avatar asked Oct 18 '22 13:10

daxterss


1 Answers

If you want to do it this way, then you can do something like:

nap=30; slept=0
while [ ! -f ./stop.dat ] && ((slept<500)); do 
    sleep $nap; 
    slept=$((slept+nap))
done

Using inotifywait instead of polling would be a more proper way of doing it.

like image 175
PSkocik Avatar answered Oct 20 '22 10:10

PSkocik