Skip to content

Instantly share code, notes, and snippets.

@bipinu
Forked from amsnickw/crontab_wrapper
Created August 19, 2016 08:30
Show Gist options
  • Save bipinu/eda56c24e769c87280fc6826e2d32242 to your computer and use it in GitHub Desktop.
Save bipinu/eda56c24e769c87280fc6826e2d32242 to your computer and use it in GitHub Desktop.
Safer use of crontab. Add this function to the user's ~/.profile or ~/.bashrc
crontab() {
crontab_usage() {
echo 'Usage:' >&2
echo ' $ crontab -[elr]' >&2
echo ' $ crontab ~/crons # to update your crons' >&2
}
if [ $# -eq 1 ]; then
if [ "$*" = '-' ]; then # worse than crontab -e
crontab_usage
elif [[ ! "$*" =~ ^-[a-zA-Z]$ ]]; then
/usr/bin/crontab $*
else
OPTIND=1
while getopts 'elr' cronopt; do
case "$cronopt" in
e)
(echo "'crontab -e' is disabled for $USER"
echo 'To update your crons, please edit ~/crons, then load the'
echo 'updated file:'
echo
echo ' $ crontab ~/crons'
echo
echo 'If this file does not yet exist (or to ensure it is up to date!)'
echo 'you can create it as follows:'
echo
echo ' $ crontab -l >~/crons') >&2
;;
r)
(echo 'You may not delete the crontab. To make changes, edit'
echo '~/crons (which may be empty) and load the updated file:'
echo
echo ' $ crontab ~/crons') >&2
;;
l)
/usr/bin/crontab -l
;;
*)
crontab_usage
;;
esac
done
OPTIND=1
fi
else
crontab_usage
fi
unset -f crontab_usage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment