1

I'm working on a bash script that will run for appoximately 30 minutes at a time. I've got it running stable as far as that part goes. I've been looking for a way to make it fire certain commands at inervals of every 3 minutes while running. I've not had any luck, so I turn to those of you that may know more about bash than I.

Any suggestions?

Here is what I have in mind of doing.

START=$(date +%s);

while read LINE <&3; do
END=$(date +%s);
if [[ $(($END-$START)) > 180 || $(($END-$START)) == 180  ]]
then
$START=$(date +%s);
run command
fi
done
user1305988
  • 119
  • 2
  • 13
  • Please [edit] your question to show [what you have tried so far](http://whathaveyoutried.com). You should include a [mcve] of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Aug 22 '16 at 15:09

4 Answers4

2

Add a cron job to make it run every 3 minutes.

*/3 * * * * /path/to/script
Thirupathi Thangavel
  • 2,418
  • 3
  • 29
  • 49
1

What about the watch command?? (https://unix.stackexchange.com/questions/10646/repeat-a-unix-command-every-x-seconds-forever)

(Second answer on here: Run command every second)

Community
  • 1
  • 1
user1775718
  • 1,499
  • 2
  • 19
  • 32
1

You can run a loop in the background:

{ while /bin/true; do some_command; sleep 180; done; } &
loop_pid=$!

Then before the main script exits, kill the background loop:

kill $loop_pid
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can also call the same script from same script.

$ cat script.sh

#!/bin/bash

# commands
# commands

sleep 1800
sh $0
Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27