Skip to content

Instantly share code, notes, and snippets.

@pacohope
Last active August 13, 2023 01:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pacohope/599bb66594ed197c2a3e2d8bfafe3e56 to your computer and use it in GitHub Desktop.
Save pacohope/599bb66594ed197c2a3e2d8bfafe3e56 to your computer and use it in GitHub Desktop.
Creates a single script to invoke a random delay on FreeBSD during execution of periodic(8) jobs. Updates /etc/crontab too
#!/bin/sh
# make delay periodic
# Called interactively as non-root user who has permission to sudo(8)
#
# Invoke interactively this way: fetch -o - https://gist.githubusercontent.com/pacohope/599bb66594ed197c2a3e2d8bfafe3e56/raw/8853effe2a325abedfd95bdd02219238343ce966/make-periodic-delay.sh | sh
# then type sudo password once (if password is required)
#
# Inspired by this thread:
# https://forums.freebsd.org/threads/delay-or-stagger-running-of-periodic-scripts-randomly.69641/
sudo mkdir -p /usr/local/etc/periodic/delay
cat - > /tmp/000-delay.sh <<EOF
#!/bin/sh
DELAY=\$(jot -r 1 0 3600)
sleep \$DELAY
EOF
sudo cp /tmp/000-delay.sh /usr/local/etc/periodic/delay
sudo chmod 755 /usr/local/etc/periodic/delay/000-delay.sh
sudo perl -pi -e 's,periodic *(.*ly)$,periodic delay $1,g' /etc/crontab
rm -f /tmp/000-delay.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment