Skip to content

Instantly share code, notes, and snippets.

@jparrill
Last active December 14, 2015 13:58
Show Gist options
  • Save jparrill/5097320 to your computer and use it in GitHub Desktop.
Save jparrill/5097320 to your computer and use it in GitHub Desktop.
Crontab eraser for automatic remove of crontab entries.
#!/bin/bash
#
#########################################
##Autor: Juan Manuel Parrilla
##Descr: This script will remove a especific crontab entry from /etc/crontab file
#########################################
##
## THIS SCRIPT NEED SUDO PERMS TO MODIFY CRONTAB FILE
## How to use:
## $1 --> Pattern to find crontab entry (EG) Script name or something
CRONTAB_FILE="/etc/crontab"
CRONTAB_FILE_BCK="/etc/crontab.bck"
TMP_CRONTAB_FILE="/tmp/crontab"
PATTERN="$1"
[ -f $CRONTAB_FILE ] || (echo "The Crontab file doesn't exist, maybe this script isnt supported for your SO" && exit -1)
finder() {
entry="$(cat $CRONTAB_FILE | grep $PATTERN)"
if [ "$PATTERN" == "" ]; then
echo "You must to introduce pattern to identify the Crontab entry" && exit -1
if [ "$entry" == "" ];then
echo "Pattern not Found" && exit -1
else
echo "Founded!"
fi
fi
}
remover() {
while read line
do
if [ "$line" != "$entry" ];then
echo "$line" > $TMP_CRONTAB_FILE
fi
done < $CRONTAB_FILE
}
replace() {
echo "Making a Backup of your Crontab file"
su - root -c "mv $CRONTAB_FILE $CRONTAB_FILE_BCK"
echo "Replacing..."
su - root -c "mv $TMP_CRONTAB_FILE $CRONTAB_FILE "
}
finder
remover
replace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment