RandomCrontab » History » Version 1
Dominik Meyer, 12/23/2024 08:01 PM
| 1 | 1 | Dominik Meyer | # Create a random crontab |
|---|---|---|---|
| 2 | |||
| 3 | Within drone it is not possible to create a cronjob for a repository in a random manner. If you select |
||
| 4 | *@weeky* or *@daily* these jobs will be all started on midnight on the specific date. If you only |
||
| 5 | have so much computing power available (for example in a kubernetes cluster) this can create some |
||
| 6 | serious problems. To overcome this problem, the following script creates a cronjob at a random time |
||
| 7 | and random day (in case of a weekly crontab). |
||
| 8 | |||
| 9 | **Requirements:** |
||
| 10 | - installed and configured drone cli tool |
||
| 11 | - linux |
||
| 12 | - bash |
||
| 13 | |||
| 14 | ` |
||
| 15 | #!/bin/bash |
||
| 16 | |||
| 17 | OCCURANCE=$1 |
||
| 18 | REPO=$2 |
||
| 19 | BRANCH=$3 |
||
| 20 | CNAME=$4 |
||
| 21 | |||
| 22 | |||
| 23 | if [ -z $CNAME ]; then |
||
| 24 | echo "Usage: droneCreateRandomCron.sh <occurance> <repository> <branch> <crontab name>" |
||
| 25 | echo -e "\t occurance = 1 = Daily | 0 = Weekly" |
||
| 26 | exit 1 |
||
| 27 | fi |
||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | HOUR=$(($RANDOM % 24)) |
||
| 32 | |||
| 33 | if [ $1 -eq 1 ]; then |
||
| 34 | |||
| 35 | DAY='*' |
||
| 36 | |||
| 37 | else |
||
| 38 | |||
| 39 | DAY=$(($RANDOM % 6)) |
||
| 40 | |||
| 41 | fi |
||
| 42 | |||
| 43 | CRONTAB="0 0 ${HOUR} * * ${DAY}" |
||
| 44 | echo "Crontab: ${CRONTAB}" |
||
| 45 | |||
| 46 | drone cron add --branch $BRANCH $REPO $CNAME "${CRONTAB}" |
||
| 47 | ` |