Siva Krishna
Siva Krishna

Reputation: 29

Reading current time and increment 1 min for next 2 hrs and run some commands

  1. I have to read current system time and increment 1 min for next 2 hrs. I need to run few commands continuously for 2 hrs with 1 min interval.

for example: current time : 8:30 PM i need to run for next 2 hrs for 8.30, 8.31.... so on for 2 hrs. I am expecting this run should print 120 output results. please help.

Need this in shell script.

Upvotes: 0

Views: 28

Answers (2)

Mark
Mark

Reputation: 4455

Here's a quick and dirty answer, if you don't mind hard coding 120 minutes in the for expression:

for min in {1..120}
do
  command &   # launch in background to minimize drift
  sleep 60
done

Alternatively, modify crontab entry (avoid all drift considerations):

crontab -l > /tmp/oldcron.$$
crontab << EOF
$(crontab -l )
*/2  * * * * command  # run command every 2 minutes forever
EOF
sleep $(( 2 * 60 * 60 ))   # sleep 2 hours
crontab < /tmp/oldcron.$$

The above probably suffers by an off-by-1 type of error. You may need to add 59 seconds to the sleep.

Upvotes: 1

user6435535
user6435535

Reputation:

#!/usr/bin/env bash

count=0
max=120 # 2 * 60 minutes

while [ true ]; do
  ((count++))
  sleep 60
  # terminal -e command
  if (( $count > $max )); then
    break
  fi
done

Upvotes: 0

Related Questions