Created
February 27, 2013 04:10
-
-
Save ankurcha/5044992 to your computer and use it in GitHub Desktop.
This file contains 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 | |
if [[ $# -eq 0 ]]; then | |
echo "Schedules a command to be run after the next reboot." | |
echo "Usage: $(basename $0) <command>" | |
echo " $(basename $0) -p <path> <command>" | |
echo " $(basename $0) -r <command>" | |
else | |
REMOVE=0 | |
COMMAND=${!#} | |
SCRIPTPATH=$PATH | |
while getopts ":r:p:" optionName; do | |
case "$optionName" in | |
r) REMOVE=1; COMMAND=$OPTARG;; | |
p) SCRIPTPATH=$OPTARG;; | |
esac | |
done | |
SCRIPT="${HOME}/.$(basename $0)_$(echo $COMMAND | sed 's/[^a-zA-Z0-9_]/_/g')" | |
if [[ ! -f $SCRIPT ]]; then | |
echo "PATH=$SCRIPTPATH" >> $SCRIPT | |
echo "cd $(pwd)" >> $SCRIPT | |
echo "logger -t $(basename $0) -p local3.info \"COMMAND=$COMMAND ; USER=\$(whoami) ($(logname)) ; PWD=$(pwd) ; PATH=\$PATH\"" >> $SCRIPT | |
echo "$COMMAND | logger -t $(basename $0) -p local3.info" >> $SCRIPT | |
echo "$0 -r \"$(echo $COMMAND | sed 's/\"/\\\"/g')\"" >> $SCRIPT | |
chmod +x $SCRIPT | |
fi | |
CRONTAB="${HOME}/.$(basename $0)_temp_crontab_$RANDOM" | |
ENTRY="@reboot $SCRIPT" | |
echo "$(crontab -l 2>/dev/null)" | grep -v "$ENTRY" | grep -v "^# DO NOT EDIT THIS FILE - edit the master and reinstall.$" | grep -v "^# ([^ ]* installed on [^)]*)$" | grep -v "^# (Cron version [^$]*\$[^$]*\$)$" > $CRONTAB | |
if [[ $REMOVE -eq 0 ]]; then | |
echo "$ENTRY" >> $CRONTAB | |
fi | |
crontab $CRONTAB | |
rm $CRONTAB | |
if [[ $REMOVE -ne 0 ]]; then | |
rm $SCRIPT | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment