Skip to content

Instantly share code, notes, and snippets.

@charles-cooper
Created June 30, 2016 14:10

Revisions

  1. charles-cooper created this gist Jun 30, 2016.
    13 changes: 13 additions & 0 deletions shutdown_idle.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    #!/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