Skip to content

Instantly share code, notes, and snippets.

@starkers
Created June 26, 2015 11:45
Show Gist options
  • Select an option

  • Save starkers/7d0260f09a43bdbd5379 to your computer and use it in GitHub Desktop.

Select an option

Save starkers/7d0260f09a43bdbd5379 to your computer and use it in GitHub Desktop.
randomly staggered crontask
#!/usr/bin/env bash
# This script allows me to run a crontab at a random point in the next hour.
# WHY?
# Using something like a little delay can also stop ALL the servers hammering a proxy at once for example.
# ...or if something breaks I don't take the entire cluster down at once
# Do the task at some random point within the next X minutes:
X=60
#pick a random minute between 1 and <=$X
TIME_TO_START=$((RANDOM%$X+1))
# put your task here
task(){
#prune files older than a month
find /var/log/httpd/ -type f -mtime +30 -exec rm -f {} \;
#restart apache for good measure, (if it breaks I like to know asap)
service httpd graceful
}
#loop once per min
for a in `seq 0 $TIME_TO_START` ; do
# echo "sleeping, $a ($TIME_TO_START)"
sleep 60
done
task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment