Herunterfahren bei Inaktivität
Ich habe meinen Linux-Server nun ab und zu für Down- bzw Uploadtätigkeiten gebraucht. Damit dieser, nachdem diese Tätigkeiten abgeschlossen waren nicht endlos nutzlos weiterlief, habe ich mir dieses Skript geschrieben um dieses Herunterfahren zu erledigen, falls kein Netzwerktraffic mehr da ist. Das folgende Skript kann man also z.B. per cron-Task einmal stündlich anstoßen.#!/bin/bash # --------------------------------------------------------------------------- # Poweroff, wenn der Durchschnittliche Traffic auf der Maschine eine gewisse # Grenze nicht erreicht. # # Derzeit: 5 Minute rx/tx messen # 1 KB/s als Schwelle für tx und rx # --------------------------------------------------------------------------- IF=enp1s0 BYTES_IN=`/home/bobo/bin/transferedBytes $IF rx 300` BYTES_OUT=`/home/bobo/bin/transferedBytes $IF tx 300` LAST_BYTES_IN=`cat /tmp/bytes_last_${IF}_rx` LAST_BYTES_OUT=`cat /tmp/bytes_last_${IF}_tx` LAST_MB_IN=`expr \( $LAST_BYTES_IN / 1024 / 1024 \)` LAST_MB_OUT=`expr \( $LAST_BYTES_OUT / 1024 / 1024 \)` DATE=`date` ACTIVE_USERS=`users | wc -w` MIN_IN=1024 MIN_OUT=1024 EMAIL=meine@emailadresse.de if [ -e /tmp/no-shutdown ]; then echo 'Der KellerPC wurden nicht heruntergefahren, da /tmp/no-shutdown existiert' \ | mail -a 'Content-Type: text/html' -s "Still working message" $EMAIL logger 'Datei /tmp/no-shutdown existiert: Kein Herunterfahren' elif [ $BYTES_OUT -le $MIN_OUT ] && [ $BYTES_IN -le $MIN_IN ] && [ $ACTIVE_USERS -eq 0 ] ; then echo $'Der KellerPC wurde wegen geringen Traffics heruntergefahren.
Datum: '$DATE'
Empfangen: | '$BYTES_IN' Bytes/s |
'$LAST_MB_IN' MB seit der letzten Messung | |
Senden: | '$BYTES_OUT' Bytes/s |
'$LAST_MB_OUT' MB seit der letzten Messung |
Bye, bye' \ | mail -a 'Content-Type: text/html' -s "Shutdown message" $EMAIL logger "Wenig Traffic (rx:$BYTES_IN, tx:$BYTES_OUT) - Herunterfahren" sleep 10 poweroff else echo $'Der KellerPC wurde wegen genügend Traffics nicht heruntergefahren. Möglicherweise sind auch Benutzer aktiv. Aktive Benutzer: '$ACTIVE_USERS'
Datum: '$DATE'
Empfangen: | '$BYTES_IN' Bytes/s |
'$LAST_MB_IN' MB seit Systemstart | |
Senden: | '$BYTES_OUT' Bytes/s |
'$LAST_MB_OUT' MB seit Systemstart |
#!/bin/bash # --------------------------------------------------------------------------------- # Read amount of transfered bytes on a network interface within a given time period # # Works at least with net-tools 2.10-alpha # --------------------------------------------------------------------------------- if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ] ; then echo "usage $0(tx|rx) " exit 1 fi function getBytes { if [ "$2" = "tx" ] ; then local START=`ifconfig $1 | egrep -o 'TX packets [0-9]+ bytes [0-9]+' | sed -E 's/TX packets [0-9]+ bytes ([0-9]+)/\1/g'` fi if [ "$2" = "rx" ] ; then local START=`ifconfig $1 | egrep -o 'RX packets [0-9]+ bytes [0-9]+' | sed -E 's/RX packets [0-9]+ bytes ([0-9]+)/\1/g'` fi echo $START } function getBytesPerSecond { local SLEEP=$3 local DATE=`date --iso=seconds` local START=`getBytes "$1" "$2"` local LAST=0 if [ -e /tmp/bytes_last_$1_$2 ]; then LAST=`cat /tmp/bytes_last_$1_$2` fi local DIFF_SINCE_LAST=`expr \( "$START" - "$LAST" \)` echo $START > /tmp/bytes_last_$1_$2 echo $DIFF_SINCE_LAST > /tmp/bytes_diff_$1_$2 sleep $SLEEP local END=`getBytes "$1" "$2"` local DIFF=`expr \( "$END" - "$START" \) / "$SLEEP"` echo $DIFF } DIFF=`getBytesPerSecond "$1" "$2" "$3"` echo $DIFF