admin:linux:lightsout

Server bei Inaktivität schlafen legen ("Lights-Out")

/usr/local/bin/lightsout
#!/bin/bash
 
# interface to watch
interface=eno1
# threshold packet count under which the script assumes IDLE
threshold=100
 
 
#######################################################################
### DO NOT CHANGE ANYTHING BELOW THIS LINE ############################
countfile="/tmp/lightsout_${interface}_count"
printf "Interface:\t\t$interface\n"
if [[ -f $countfile ]]
  then
    changed=`date -r $countfile +%s`
    printf "Last record:\t\t$(date -d @$changed)\n"
  else
    printf 0 > $countfile
fi
sent=`cat /sys/class/net/$interface/statistics/tx_packets`
received=`cat /sys/class/net/$interface/statistics/rx_packets`
let "total = $sent + $received"
let "difference = $total - $(cat $countfile)"
printf "Total packets Rx+Tx:\t$total\n"
if (( "$difference" > "$threshold" ));
  then
    printf "Packet count:\t\t$difference\n"
    printf $total > $countfile
  else
    let "datediff = $(date +%s) - $changed"
    printf "IDLE seconds:\t\t$datediff\n"
    if (( "$datediff" > 3600 )); then
        rm $countfile
        printf "suspending...\n"
        systemctl suspend
    fi
fi
  • Last modified: 2019-12-20 14:21