Skip to content

Instantly share code, notes, and snippets.

@amsnickw
Created August 19, 2016 08:29
Show Gist options
  • Save amsnickw/a9f06f752f4e226329bb301029533091 to your computer and use it in GitHub Desktop.
Save amsnickw/a9f06f752f4e226329bb301029533091 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