简体   繁体   中英

bash timer in milliseconds

I am trying to implement a timer for 5mins(300secs) in millisecons, but it is taking more than 5 minutes for sleep 0.001.

If I change if [ $t -ge 300000 ] to if [ $t -ge 30000 ] , it is completing within 3 minutes.

df1, df2 holds the value of disk usage value at time t1 and t2 respectively.

t=0
x=0
df1
    while [ $x -lt 10000 ]

        sleep 0.001
        df2
        x=(( df1-df2 ))
        if [ $t -ge 300000 ]
        then
        t=0
        df1            
        else
        (( t++ ))
        fi
    done
dosomething

I want test $x for every 0.001secs. df1 and df2 should hold new values for every 300 secs. how can I achieve this?

date %N, will get time in nano seconds, maybe this help

start_at=$(date +%s,%N)
_s1=$(echo $start_at | cut -d',' -f1)   # sec
_s2=$(echo $start_at | cut -d',' -f2)   # nano sec
# do somethong here
end_at=$(date +%s,%N)
_e1=$(echo $end_at | cut -d',' -f1)
_e2=$(echo $end_at | cut -d',' -f2)
time_cost=$(bc <<< "scale=3; $_e1 - $_s1 + ($_e2 -$_s2)/1000000000")

I'm pretty sure that a single sleep like yours does not take several minutes. While it can and, in general, will take longer than the milisecond which you requested, the real sleep time should still be well below one second, or, if the system is heavily loaded, at most a couple of seconds.

One flaw in your application is that you don't take into account how long one iteration of your loop takes. You can't know this in advance; you only know that it will be longer than a milisecond, but not how much longer. Hence, you should end the loop after a certain time has passed - ie storing the start time somewhere and comparing the current time to the start time.

A second problem is that you don't set anywhere the variables df1 and df2, but maybe you left out this detail because you didn't consider it relevant for your question.

If you are looking for a countdown timer which displays milli-sec, you are looking for this.

This shows timer to hundredth of a second.

Change the logic as required.

    timer() {
    time="`date '+%H:%M:%S' -d "$1"`"
    if [ $# -gt 0 ] && [ "$1" = "$time" ]; then
        echo 
        IFS=:
        set -- $*
        secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} ))
        while [ $secs -ge 0 ]; do
                for msec in {99..0}; do
                printf "\rCountdown Timer: %02d:%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60))  $((msec))
                sleep .01
                done
                secs=$(( $secs - 1 ))
        done
        echo
    fi
    echo 
    }

    Usage: # timer HH:MM:SS
    Where: HH  Time in Hours
           MM  Time in Mins
           SS  Time in Secs
    Example: 
           # timer 00:10:00 
           Sets a countdown timer for 10 mins while displaying hundredth of a second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM