Henry
Henry

Reputation: 1

random cron job

I need a script1 that will execute script2 at random times a day. I'm looking to execute the script2 around 30 times a day within random times. script1 will be set in the cron job. Could someone please help how to make it happen? PS I am not a programmer, so would need something ready to go, please

Upvotes: 0

Views: 2029

Answers (2)

Speckinius Flecksis
Speckinius Flecksis

Reputation: 624

Seth's solution certainly works, but the number of executions per day will differ. If you want definitely 30 executions, not more and not less, I propose using a cron entry like

0 0 * * * gen-executions.sh

and a script gen-executions.sh:

#!/bin/bash
for number in $(seq 30)
do
    hour=$(( ${RANDOM}*24/32768 ))
    minute=$(( ${RANDOM}*60/32768 ))
    at -f /path/to/script.sh $(printf "%02d" ${hour}):$(printf "%02d" ${minute})
done

This generates exactly 30 executions of /path/to/script.sh at random times of the day using at.

Upvotes: 7

Seth Robertson
Seth Robertson

Reputation: 31461

* * * * * script1.sh

#!/bin/bash
if [ $(($RANDOM*100/32768)) -gt 2 ]; then exit; fi
exec php script2.php

Upvotes: 2

Related Questions