Skip to content

Instantly share code, notes, and snippets.

@hydrastro
Last active October 23, 2021 16:19
Show Gist options
  • Select an option

  • Save hydrastro/529238468f4cc697852e6af5d2042a5b to your computer and use it in GitHub Desktop.

Select an option

Save hydrastro/529238468f4cc697852e6af5d2042a5b to your computer and use it in GitHub Desktop.
dead_switch.sh
#!/bin/bash
# Add this script to crontab:
# `sudo printf "33 3\t* * *\troot\t/etc/dead_switch.sh" >> /etc/crontab`
# Dead Switch configuration
your_email=""
server_username=""
warning_time=$((3600 * 24 * 14))
trigger_time=$((3600 * 24 * 28))
# Emails
warning_email_title="[WARNING] Dead Switch"
warning_email_body="Server feels alone, please log in."
critical_email_title="[CRITICAL] Dead Swtich"
critical_email_body=$(cat <<EOF
Log in immediately.
The dead switch will be triggered within 24 hours.
EOF
)
farewell_email_title="Farewell"
farewell_email_body="It's been nice."
last_login_time=$(last -1 -R "$server_username" --time-format iso | \
awk '{print $3}' | date -d "$1" +"%s")
current_time=$(date +%s)
if [[ $current_time -gt $((last_login_time + warning_time)) ]]; then
send_mail "$your_email" "$warning_email_title" "$warning_email_body"
fi
if [[ $current_time -gt $((last_login_time + trigger_time - 86400)) ]]; then
send_mail "$your_email" "$critical_email_title" "$critical_email_body"
fi
if [[ $current_time -gt $((last_login_time + trigger_time)) ]]; then
send_mail "$your_email" "$farewell_email_title" "$farewell_email_body"
# Here you do your stuff. Example:
# send_mail "[email protected]" "[IMPORTANT] Dead Switch" \
# "Anon is dead.\nHis last words: good luck lmao."
# srm -rf /
sed -i '/dead_switch/s/^/#/g' /etc/crontab
fi
@hydrastro
Copy link
Author

hydrastro commented Oct 22, 2021

send_mail is defined here: https://gist.github.com/hydrastro/5ccfc0dc87a8bb3c5ec9062a3cba5bf6

sed -i '/dead_switch/s/^/#/g' /etc/crontab comments out the crontab entry for the dead switch, so it doesn't run twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment