Skip to content

Instantly share code, notes, and snippets.

@compor
Created February 26, 2013 11:33
Show Gist options
  • Select an option

  • Save compor/5037836 to your computer and use it in GitHub Desktop.

Select an option

Save compor/5037836 to your computer and use it in GitHub Desktop.
add/remove crontab entry
#!/bin/bash
# it can add/delete multiple times the same crontab entry
# without having to worry for duplicate entries (in case of adding)
# or any leftovers (in case of deleting)
# THIS KEY HAS TO BE UNIQUE IN THE ENTRY TO BE ADDED/DELETED
CMD_KEY="moo"
CRONTAB="/etc/crontab"
COUNT=`grep -c ${CMD_KEY} ${CRONTAB}`;
case "$1" in
add)
if [ ${COUNT} -ne 0 ]; then
echo "entry has already been added"
exit 0
else
# EDIT THIS WITH YOUR ENTRY
echo "0-59/5 * * * * root nohup /root/moo.sh &> /dev/null" >> ${CRONTAB}
exit $?
fi
;;
del)
if [ ${COUNT} -ne 0 ]; then
sed -i '/'${CMD_KEY}'/d' ${CRONTAB}
exit $?
else
echo "entry has already been deleted"
exit $?
fi
;;
*)
echo "unrecognized option"
exit 2
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment