user1897949
user1897949

Reputation:

Schedule operation bash

I need to compute N operation (same operation) each hour.

For the moment I have my operations but I can't understand execute it N time per hour:

 #!/bin/bash
 N=10; 
 # my operation
 sleep 3600/${N}

Any suggestion?

Upvotes: 0

Views: 201

Answers (1)

twalberg
twalberg

Reputation: 62389

Well, the way you have it written now, it will only perform "my operation" once, after which it will sleep for 6 minutes, and then exit. What you need is a loop construct around those two lines. bash supports several different loop constructs, but probably the simplest for this case would be:

#!/bin/bash
N=10
((delay=3600/N))
while true
do
  # do something
  sleep ${delay}
done

Of course, there's no provision for terminating the loop in this case, so you'll have to exit with ^C or kill or something. If you only wanted it to run a certain number of times, you could use a for loop instead, or you could have it check for the [non-]existence of a certain file each iteration and exit the loop when you create or remove that file. The most appropriate approach depends on the bigger picture of what you are really trying to do.

Upvotes: 1

Related Questions