Leo
Leo

Reputation: 63

Crontab, execute a task a random number of times within a period of time

I want to use cron to run a task a random number of times every 3 minutes and 30 seconds, between 8 a.m. and 6 p.m. They could help me with this problem. I've been searching, but I have not managed to do it. Thank you very much in advance.

Upvotes: 1

Views: 365

Answers (1)

sashimi
sashimi

Reputation: 1304

I would address the issue two ways - depending on the requirement:

  • if you mean that a task may be executed or not at random, every 3 minutes and 30 seconds between 8am and 6pm, you can add some random number generation and execute if matches certain criteria (number greater than x or divisible by y, etc)
  • if you mean that the task should be executed random N times for each trigger occurring every 3 minutes 30 seconds between 8am and 6pm, you could use a random number to specify how many times to execute and then loop until reaching that number of executions.

As for the cron, you may find this page useful to assemble it :)

EDIT

Here a follow-up on the comment, that the use case refers to the second one I mentioned above:

script

random_times=$(( ( RANDOM % 10 )  + 1 ))
for i in `seq 1 $random_times`; do bash /path/to/script.sh; done

crontab

*/3 * * * *

Please notice, that if using unix crontab, you do not have seconds granularity and thus will execute every 3 minutes.

Upvotes: 1

Related Questions