Created
December 22, 2023 16:11
-
-
Save chigozienri/0b542134a9fef23822012b09b2c88d50 to your computer and use it in GitHub Desktop.
Shutdown remote server after 8 hours, with 1 hour warning.
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/sh | |
# Adapted from https://gist.github.com/charles-cooper/a060b7225d010d93a37a62a7b4d1e2b4 | |
# To email yourself, set up nullmailer with gmail app password (no spaces required), set up the cronjob with `sudo crontab -e`, then add the following: | |
# MAILTO="[email protected]" | |
# @hourly /home/chigozie/.local/bin/cron/shutdown_idle.sh | |
uptime=$(cat /proc/uptime | awk '{print $1}') | |
max_hours=7 | |
if [ $(echo $uptime | cut -d '.' -f1) -ge $((60*60*$max_hours)) ]; then | |
# Shutdown in 1 hour | |
sudo shutdown -h +60 | |
echo "Save your work! This system will shut down in 1 hour! If you want to cancel this shutdown, use \`sudo shutdown -c\`" | |
else | |
echo $(($(echo "$uptime" | cut -d '.' -f1)/60)) | awk '{print $1" minutes of uptime; after 7 hours, the system will schedule a shutdown with 1 hour warning."}' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment