Skip to content

Instantly share code, notes, and snippets.

@jesslilly
Last active March 22, 2024 14:16
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save jesslilly/315132a59f749c11b7c6 to your computer and use it in GitHub Desktop.
Save jesslilly/315132a59f749c11b7c6 to your computer and use it in GitHub Desktop.
Deluxe Cron Job Wrapper

Cron Job Wrapper Wish List

I want a script that will give me:

  1. Logging
  2. Log purging!
  3. Email errors!
  4. Prevent duplicate processes! (flock)
  5. Source an environment file!
  6. Anything else?

Hey, FYI I set this up for a friend and never actually used the code myself. No linux box to play with ATM.

# TODO: Put your custom header info here!
# ------------------------------------------------------------------------------------
# Thanks to https://en.wikipedia.org/wiki/Cron
# ------------------------------------------------------------------------------------
# Set up a cron to run at different frequencies using a keyword, or a sequence of the form * * * * *.
# ------------------------------------------------------------------------------------
# @yearly (or @annually) Run once a year at midnight of January 1 0 0 1 1 *
# @monthly Run once a month at midnight of the first day of the month 0 0 1 * *
# @weekly Run once a week at midnight on Sunday morning 0 0 * * 0
# @daily Run once a day at midnight 0 0 * * *
# @hourly Run once an hour at the beginning of the hour 0 * * * *
# @reboot Run at startup @reboot
# ------------------------------------------------------------------------------------
#┌───────────── min (0 - 59)
#│ ┌────────────── hour (0 - 23)
#│ │ ┌─────────────── day of month (1 - 31)
#│ │ │ ┌──────────────── month (1 - 12)
#│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
#│ │ │ │ │
#│ │ │ │ │
#* * * * * command to execute
# ------------------------------------------------------------------------------------
# TODO: Add your tasks here!
# Thanks to http://serverfault.com/a/82863
* * * * * /usr/bin/flock -n /tmp/fcj.lockfile /usr/local/bin/frequent_cron_job --minutely
@daily /usr/local/bin/cronwrapper.sh /usr/local/bin/cronwrapper_purge_logs.sh /var/log/cronwrapper
# Thanks to http://saltnlight5.blogspot.com/2014/06/a-simple-cron-wrapper-script-with.html
# Options
DIR=`dirname $0`
CMD="$@"
CMD_NAME=`basename $1`
LOG_NAME=${LOG_NAME:=$CMD_NAME}
LOG="$DIR/logs/$LOG_NAME.log`date +%s`"
# Ensure logs dir exists
if [[ ! -e $DIR/logs ]]; then
mkdir -p $DIR/logs
fi
# Run cron command
source $HOME/.bash_profile
echo "`date` Started cron cmd=$CMD, logname=$LOG_NAME" >> $LOG 2>&1
$CMD >> $LOG 2>&1
echo "`date` Cron cmd is done." >> $LOG 2>&1
# Pass in the directory you want to purge as the first argument
DIR=$1
echo "Checking and removing logs in $DIR"
find $DIR -type f -mtime +31 -print -delete
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment