Skip to content

Instantly share code, notes, and snippets.

@theist
Created March 2, 2017 23:32
Show Gist options
  • Save theist/79ca8cf1701f92bf56aa2f788af703b9 to your computer and use it in GitHub Desktop.
Save theist/79ca8cf1701f92bf56aa2f788af703b9 to your computer and use it in GitHub Desktop.
quick cronshell
#!/bin/bash
CRONLOG=/var/log/cron.log
RID=$(cat /dev/urandom | tr -dc "[:alpha:]" | head -c 8)
CMD=$@
START=$(date +%s)
echo "---- CRON --- START at $(date) --------" | ts | sed "s/^/[${RID}] /" >> ${CRONLOG}
echo "-- CMD: $@ ----" | ts | sed "s/^/[${RID}] /" >> ${CRONLOG}
/bin/bash -c "$@" > >( sed 's/^//') 2> >( sed 's/^/ [ERR]: /' ) | ts | sed "s/^/[${RID}] /" >> ${CRONLOG}
RETURN=$?
END=$(date +%s)
ELAPSED=$(( $END - $START ))
echo "-- CMD: $@ - EXITSTATUS:${RETURN}---" | ts | sed "s/^/[${RID}] /" >> ${CRONLOG}
echo "---- CRON --- END at $(date) (${ELAPSED} s elapsed)--------" | ts | sed "s/^/[${RID}] /" >> ${CRONLOG}
# use in cron as /path/to/cronshell 'bash one line of commands between single quotes, may contain semicolons'
# YMMV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment