Skip to content

Instantly share code, notes, and snippets.

@charles-cooper
Created June 30, 2016 14:10
Show Gist options
  • Save charles-cooper/a060b7225d010d93a37a62a7b4d1e2b4 to your computer and use it in GitHub Desktop.
Save charles-cooper/a060b7225d010d93a37a62a7b4d1e2b4 to your computer and use it in GitHub Desktop.
Cron job to shut down an idle computer
#!/bin/sh
# This script, intended to be run as a cron job, checks if there are any logins or recent logins and shuts down if there are none. It is not intended to be portable and was written on a Ubuntu 16.04 machine.
last_login_time=$(grep "systemd-logind" /var/log/auth.log | perl -pe "s/ ip.*//" | tail -1 | date --file=/dev/stdin +%s)
if [ $(($cur_time - $last_login_time)) -ge 3600 ] && [ $(who | wc -l) -eq 0 ]
then
init 0
else
echo 'current logins:' > /home/ubuntu/logins
who >> /home/ubuntu/logins
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment