2

I want to print by echo the following "still in process wait another 10 min" every 10 min

Please advice what condition needs to add before the echo command in order to print this line every 10 min?

Remark - counter increase by one every cycle (1 second) , I not want to add Additional delay ( sleep command ) to this script !!!!!

 Until   [    ]
 do
 Counter=1

 sleep 1

  let counter=$counter+1

  [ .... ] &&  echo " still in process wait another 10 min …."

 done 

5 Answers 5

1

Add another counter, and if that reaches 600, call echo and reset the counter. Repeat as needed.

So something like this should do:

let echocounter=$echocounter+1
if [ $echocounter == 600 ]; then echo "still in process wait 10 min ..."; $echocounter=0; fi
2
  • Just remove the $ before echocounter ( $echocounter=0; fi not ilegal in bash)
    – yael
    Jun 11, 2012 at 13:26
  • 1
    +1 this is what i would have suggested. take loop until counter is 600, then echo thing and reset counter to 0. however, from faker's answer, i realize mod is much more elegant and efficient
    – acolyte
    Jun 11, 2012 at 13:55
7

Use the modulo operator (bash specific syntax):

if [ $(($counter % 600)) -eq 0 ] ; then
  echo " still in process wait another 10 min .."
fi

Or more portable:

if [ `expr $counter % 600` -eq 0 ] ; then
  echo " still in process wait another 10 min .."
fi
1
  • 1
    Aieeee! Should have thought this.... +1 to you, sir! Jun 11, 2012 at 13:28
1

What you want, is something like this:

#10 minutes from now
Increment_Timestamp=$(( $(date +%s) + 600 ))

while [  ...  ]
do
...
...
    # When now is 10 minutes ago, show the message.
    if [ $(date +%s) -lt "${Increment_Timestamp}" ]
    then
        ${Increment_Timestamp}=$(( $(date +%s) + 600 ))
        echo "Message"
    fi
done
0

I added some logic to check the time:

BOOL=true
MINUTES=$(date +"%M")
MINUTES=$(echo $MINUTES | sed 's/^0*//')

if $BOOL ; then
        BOOL=false

        if [ $MINUTES -le "49" ];then
                MINUTES10=$(($MINUTES+10))
        else
                MINUTES10=$(($MINUTES+50))
        fi
fi

if [ $MINUTES10 -eq $MINUTES ];then
        echo "message"
        BOOL=true
fi
3
  • hi - I not want to add another delay to my script ( not want to add another sleep )
    – yael
    Jun 11, 2012 at 12:35
  • Next time provide a bit more information what you want, your question is not really clear. So you want to do process X and every ten minutes you want to print the message if the script hasnt finished? Jun 11, 2012 at 12:40
  • yes I want to create condition - see my comment in my question
    – yael
    Jun 11, 2012 at 12:41
0

You probably want the sleepenh program, which allows for "accurate sleeping". There's also a blog post about how it was used to solve a problem which required no oversleeping

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .