Created
February 26, 2013 11:33
-
-
Save compor/5037836 to your computer and use it in GitHub Desktop.
add/remove crontab entry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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