How to fixate on a certain time?

I am creating a script that should wait for a specific file to appear (e.g. stop.dat ) or after a certain time (e.g. 500 seconds).

I know how to wait for a file to appear:

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

How to add another statement to my while loop?

+5
source share
2 answers

If you want to do it this way, 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 better way to do this.

+2
source

you can remember the time and compare with the current time in the loop state

 echo -n "before: " date t1=$(( $(date +"%s" ) + 15 )) #15 for testing or … your 500s later while [ $(date +"%s") -lt $t1 ] #add other condition with -a inside the [ ] which is shortcut for `test` command, in case you want to use `man` do sleep 3 #do stuff done echo -n "after: " date 
+1
source

Source: https://habr.com/ru/post/1267233/


All Articles