Skip to content

Commit

Permalink
Merge pull request #46 from rhyeal/bugfix/debian-crontab-cmd-path
Browse files Browse the repository at this point in the history
Fix path to script in Debian crontab
  • Loading branch information
mmrwoods authored Jul 29, 2020
2 parents b89aeee + e20dc6d commit 2efc8f2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.8.2
0.9.8.3
19 changes: 10 additions & 9 deletions src/debian/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@ echo "SUDO_USER: $SUDO_USER"

# Let's cycle through to find a non blank and non-root username
if [[ "$(ps -o user= -p $$ | awk '{print $1}')" != "root" && "$(ps -o user= -p $$ | awk '{print $1}')" != "" ]]; then
MY_USER=$(ps -o user= -p $$ | awk '{print $1}')
MY_USER=$(ps -o user= -p $$ | awk '{print $1}')
elif [[ "$(logname)" != "root" && "$(logname)" != "" ]]; then
MY_USER=$(logname)
MY_USER=$(logname)
elif [[ "$(whoami)" != "root" && "$(whoami)" != "" ]]; then
MY_USER=$(whoami)
MY_USER=$(whoami)
elif [[ "$(id -u -n)" != "root" && "$(id -u -n)" != "" ]]; then
MY_USER=$(id -u -n)
MY_USER=$(id -u -n)
elif [[ "$USER" != "root" && "$USER" != "" ]]; then
MY_USER=$USER
MY_USER=$USER
elif [[ "$SUDO_USER" != "root" && "$SUDO_USER" != "" ]]; then
MY_USER=$SUDO_USER
MY_USER=$SUDO_USER
else
echo "Cannot find the username! Skipping the crontab install"
fi

if [[ "$MY_USER" != "" ]]; then
IS_ENABLED=$(crontab -u $MY_USER -l | grep '/usr/bin/aws-rotate-iam-keys' | wc -l)
SCRIPT_PATH=$(which aws-rotate-iam-keys 2>/dev/null || echo /usr/bin/aws-rotate-iam-keys)
IS_ENABLED=$(crontab -u $MY_USER -l | fgrep $SCRIPT_PATH | wc -l)
if [[ "$IS_ENABLED" == 0 ]]; then
line="$((RANDOM%60)) 2 * * * /usr/bin/aws-rotate-iam-keys --profile default >/dev/null #rotate AWS keys daily"
(crontab -u $MY_USER -l; echo "$line" ) | crontab -u $MY_USER -
LINE="$((RANDOM%60)) 2 * * * $SCRIPT_PATH --profile default >/dev/null #rotate AWS keys daily"
(crontab -u $MY_USER -l; echo "$LINE" ) | crontab -u $MY_USER -
fi
fi
20 changes: 11 additions & 9 deletions src/debian/prerm
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env bash

MY_USER=""

echo "Trying to find a user name for the cron job"
echo "ps: $(ps -o user= -p $$ | awk '{print $1}')"
echo "logname: $(logname)"
Expand All @@ -12,21 +10,25 @@ echo "SUDO_USER: $SUDO_USER"

# Let's cycle through to find a non blank and non-root username
if [[ "$(ps -o user= -p $$ | awk '{print $1}')" != "root" && "$(ps -o user= -p $$ | awk '{print $1}')" != "" ]]; then
MY_USER=$(ps -o user= -p $$ | awk '{print $1}')
MY_USER=$(ps -o user= -p $$ | awk '{print $1}')
elif [[ "$(logname)" != "root" && "$(logname)" != "" ]]; then
MY_USER=$(logname)
MY_USER=$(logname)
elif [[ "$(whoami)" != "root" && "$(whoami)" != "" ]]; then
MY_USER=$(whoami)
MY_USER=$(whoami)
elif [[ "$(id -u -n)" != "root" && "$(id -u -n)" != "" ]]; then
MY_USER=$(id -u -n)
MY_USER=$(id -u -n)
elif [[ "$USER" != "root" && "$USER" != "" ]]; then
MY_USER=$USER
MY_USER=$USER
elif [[ "$SUDO_USER" != "root" && "$SUDO_USER" != "" ]]; then
MY_USER=$SUDO_USER
MY_USER=$SUDO_USER
else
echo "Cannot find the username! Skipping the crontab removal"
fi

if [[ "$MY_USER" != "" ]]; then # if this is being run as a user, remove this line from cron tab
crontab -u $MY_USER -l | grep -v '/usr/bin/aws-rotate-iam-keys --profile default' | crontab -u $MY_USER -
SCRIPT_PATH=$(which aws-rotate-iam-keys 2>/dev/null || echo /usr/bin/aws-rotate-iam-keys)
IS_ENABLED=$(crontab -u $MY_USER -l | fgrep $SCRIPT_PATH | wc -l)
if [[ "$IS_ENABLED" == 0 ]]; then
crontab -u $MY_USER -l | grep -v $SCRIPT_PATH --profile default | crontab -u $MY_USER -
fi
fi

0 comments on commit 2efc8f2

Please sign in to comment.