Created
January 16, 2016 16:29
-
-
Save falconscript/1881102f3aab7045bb8c to your computer and use it in GitHub Desktop.
Cronjob script to auto-shutdown Macbook after 30 minutes of idling
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # Script for shutdown the mac if no user activity for specified time | |
| cd ~/Desktop | |
| # REQUIRED to run properly as a CRON job | |
| export PATH="/usr/bin:/usr/local/sbin:/usr/local/share:/usr/local/bin:/usr/bin:/bin:/usr/sbin" | |
| # Run every 5 minutes (every 5 minutes it checks to see if 30 minutes of nothing have gone by) | |
| # env EDITOR=nano crontab -e | |
| # */5 * * * * ~/Desktop/shutdown.sh | |
| TIMEOUT=$((29 * 60)) # (seconds * 60) to be idle before shutdown | |
| IDLE=`/usr/sbin/ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'` | |
| if [[ $IDLE -ge $TIMEOUT ]]; then | |
| Logger "[WARN] $0 - SHUTTING DOWN. Over $(($TIMEOUT / 60)) min idle." | |
| /usr/bin/osascript << EOF | |
| tell application "System Events" | |
| shut down | |
| end tell | |
| EOF | |
| # Force shutdown if still alive. Stronger than applescript's kill NEEDS ROOT | |
| mkdir ~/Desktop/SHUTTING_DOWN_delete_to_cancel > /dev/null 2>&1 | |
| sleep 60 | |
| if [ -d "~/Desktop/SHUTTING_DOWN_delete_to_cancel" ]; then # -d for directory | |
| Logger "[INFO] $0 - WEAK SHUTDOWN FAILED. TRYING HARD KILL." | |
| /sbin/shutdown -h now # Shutdown if not deleted | |
| else | |
| (cd "~/Desktop/SHUTTING_DOWN_delete_to_cancel" ) && Logger "SUPER WEIRD" | |
| Logger "[INFO] $0 - Inactivity hit -> confirmation folder deletion -> canceled shutdown" | |
| fi | |
| exit | |
| else | |
| Logger "[INFO] $0 - No shutdown, only $(($IDLE / 60)) min idle." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment