user1305988
user1305988

Reputation: 139

How to make bash script execute command at regular intervals

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

Upvotes: 1

Views: 2088

Answers (4)

Sriharsha Kalluru
Sriharsha Kalluru

Reputation: 1823

You can also call the same script from same script.

$ cat script.sh

#!/bin/bash

# commands
# commands

sleep 1800
sh $0

Upvotes: 0

Thirupathi Thangavel
Thirupathi Thangavel

Reputation: 2465

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

*/3 * * * * /path/to/script

Upvotes: 2

Barmar
Barmar

Reputation: 780808

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

Upvotes: 1

user1775718
user1775718

Reputation: 1578

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)

Upvotes: 1

Related Questions