Skip to content

Instantly share code, notes, and snippets.

@scr34m
Created August 19, 2016 14:07
Show Gist options
  • Save scr34m/9ef10221cc31600bf4de430f303c7726 to your computer and use it in GitHub Desktop.
Save scr34m/9ef10221cc31600bf4de430f303c7726 to your computer and use it in GitHub Desktop.
Cron wrapper for crondiary communication
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: CRONDIARY_JOB=<your job id> crondiary 'command'"
exit 1
fi
if [ -z "$API_KEY" ] && [ -e ~/.crondiary ]; then
API_KEY=`cat ~/.crondiary`
fi
if [ -z "$API_KEY" ] && [ -e /etc/crondiary.conf ]; then
API_KEY=`cat /etc/crondiary.conf`
fi
if [ -z "$API_KEY" ]; then
echo "Please specify your API key in ~/.crondiary or /etc/crondiary.conf"
exit 1
fi
crondiary() {
output=""
if [ -n "$2" ]; then
output="-F output=@$2"
fi
RESPONSE=$(curl -w '!%{http_code}' --silent -H "X-Crondiary-Key: $API_KEY" -F "n=$CRONDIARY_JOB" $output http://crondiary.com/api/$1)
STATUS=`echo $RESPONSE | sed -e 's/.*!\([0-9]*\)/\1/'`
if [ $STATUS -ne 200 ]; then
echo $RESPONSE | sed -e 's/\(.*\)![0-9]*/\1/'
fi
}
crondiary start
TMPFILE=`mktemp -t crondiary.XXXXXX`
bash -c "($@ &> $TMPFILE 2>&1)"
if [ $? -ne 0 ]; then
crondiary failed $TMPFILE
else
crondiary stop $TMPFILE
fi
unlink $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment