[[Vorlage(Getestet, , general)]] {{{#!vorlage Wissen [:Editor: Einen Editor öffnen] [:sudo: Root-Rechte] [:Rechte: Rechte für Dateien und Ordner ändern] [:Terminal: Ein Terminal öffnen] }}} [[Inhaltsverzeichnis()]] Auto OFF dient dem automatischen Abschalten eines Computers bei Nichtbenutzung. Dies ist beispielsweise sinnvoll, wenn sich ein Server nach einem bestimmten Zeitraum (z.B. 20 Minuten) ohne Zugriffe selbsttätig abschalten soll. Der Server wird per [:Wake_on_LAN: Wake On LAN] oder [#Zeitgesteuertes-Aufwachen zeitgesteuert] geweckt. = Vorbereitungen = == Cron-Job == Ein [:Cron:]-Job ruft das Skript alle 20 Minuten auf. Dazu wird eine neue Datei '''/etc/cron.d/checkshutdown''' erzeugt. Diese Datei wird in einem Editor[1] mit Root-Rechten[2] bearbeitet: {{{#!code # Auto shutdown */20 * * * * root /usr/local/sbin/checkshutdown.sh }}} == Skript == Das vom [:Cron: Cron Job] aufgerufene Skript prüft, ob * bestimmte Programme laufen * bestimmte Dämonen aktiv sind * noch Samba Verbindungen aktiv sind * User eingeloggt sind * andere PCs noch eingeschaltet sind Nur wenn zwei mal hintereinander keine Aktivität mehr vorhanden ist, wird per "`Halt`"-Befehl abgeschaltet. Das folgende Skript wird in einen Editor[4] kopiert und mit Root-Rechten[2] als '''/usr/local/sbin/checkshutdown.sh''' gespeichert. Außerdem muss es ausführbar gemacht werden[3]: {{{#!code bash #!/bin/bash # #set -x . /etc/autoshutdown.conf logit() { logger -p local0.notice -s -- AutoShutdown: $* } IsOnline() { for i in $*; do ping $i -c1 if [ "$?" == "0" ]; then logit PC $i is still active, auto shutdown terminated return 1 fi done return 0 } IsRunning() { for i in $*; do if [ $(pgrep -c $i) -gt 0 ] ; then logit $i still active, auto shutdown terminated return 1 fi done return 0 } IsDamonActive() { for i in $*; do if [ $(pgrep -c $i) -gt 1 ] ; then logit $i still active, auto shutdown terminated return 1 fi done return 0 } IsPortInUse() { for i in $*; do LANG=C netstat -an | grep -q "${myIp}:${i}.*ESTABLISHED$" Err=${?} if [ ${Err} -eq 0 ] ; then logit "Port ${i} is still in use, auto shutdown terminated" return 1 fi done return 0 } IsBusy() { # Samba if [ "x$SAMBANETWORK" != "x" ]; then if [ $(/usr/bin/smbstatus -b | grep $SAMBANETWORK | wc -l) != "0" ]; then logit samba connected, auto shutdown terminated return 1 fi fi #damons that always have one process running IsDamonActive $DAMONS if [ "$?" == "1" ]; then return 1 fi #backuppc, wget, wsus, .... IsRunning $APPLICATIONS if [ "$?" == "1" ]; then return 1 fi # check network-ports if [ "x${NETWORKPORTS}" != "x" ]; then myIp=$(LANG=C /sbin/ifconfig | sed -n "/inet addr/ { s|^[a-z ]*:\([0-9\.]*\).*$|\1|p }" | head -n 1) IsPortInUse ${NETWORKPORTS} if [ "$?" == "1" ]; then return 1 fi fi # Read logged users USERCOUNT=$(who | wc -l); # No Shutdown if there are any users logged in test $USERCOUNT -gt 0 && { logit some users still connected, auto shutdown terminated; return 1; } IsOnline $CLIENTS if [ "$?" == "1" ]; then return 1 fi return 0 } COUNTFILE="/var/spool/shutdown_counter" OFFFILE="/var/spool/shutdown_off" # turns off the auto shutdown if [ -e $OFFFILE ]; then logit auto shutdown is turned off by existents of $OFFFILE exit 0 fi if [ "$AUTO_SHUTDOWN" = "true" ] || [ "$AUTO_SHUTDOWN" = "yes" ] ; then IsBusy if [ "$?" == "0" ]; then # was it not busy already last time? Then shutdown. if [ -e $COUNTFILE ]; then # shutdown rm -f $COUNTFILE logit auto shutdown caused by cron /sbin/halt -p exit 0 else # shut down next time touch $COUNTFILE logit marked for shutdown in next try exit 0 fi else rm -f $COUNTFILE #logit aborted exit 0 fi fi logit malfunction exit 1 }}} == Konfigurationsdatei == Die Konfigurations-Datei '''/etc/autoshutdown.conf''' wird in einen Editor[4] erstellt und mit Root-Rechten[2] abgespeichert. Hier kann man eigene Programme, Dämonen, Sambanetzwerke und Client-Rechner eintragen. {{{#!code bash # Turn on auto shutdown AUTO_SHUTDOWN=yes # Damons that always have one process running, only if more that one process is active we prevent the shutdown # The values are used with grep, so just a unique portion is sufficient DAMONS="rsync BackupPC_" # Important applications that shall prevent the shutdown # The values are used with grep, so just a unique portion is sufficient APPLICATIONS="BackupPC_nightly BackupPC_dump wsus wget screen mlnet" # Network IP range for checking any open samba connections # The value is used with grep, so just a unique portion is sufficient SAMBANETWORK="192.168.1." # Names or IP for computers that shall prevent the shutdown # We ping these computers in the list to check whether they are active. CLIENTS="sonne mond stern 192.168.1.10 192.168.1.11 192.168.1.12" # List of ports # we check all of them, if there in use NETWORKPORTS="22" }}} = Dauerlauf erzwingen = Man kann den "auto shutdown" mit verschiedenen Methoden unterbinden. * Indem die Datei '''/var/spool/shutdown_off''' erzeugt wird. Es genügt dabei, dass diese Datei existiert, der Inhalt ist egal. * In der Konfigurationsdatei '''/etc/autoshutdown.conf''' den Schalter "`AUTO_SHUTDOWN=no`" aufnehmen. * Umbenennen der Datei '''/etc/cron.d/checkshutdown''' in '''/etc/cron.d/checkshutdown.off'''. Cron startet keine Dateien mit einem Punkt im Namen. = Zeitgesteuertes Aufwachen = Es existieren mehrere Möglichkeiten, den Rechner über die Uhr (RTC) aufzuwecken. Bei neuer Hardware funktioniert ACPI am besten (siehe [:rtcwake:]), bei älterer Hardware kommt man eventuell mit '''nvram-wakeup''' aus dem gleichnamigen Paket zum Ziel. * [http://www.vdr-wiki.de/wiki/index.php/Kategorie:Wakeup Wakeup] {de} - vdr-wiki.de == ACPI-Wakeup == * [http://www.vdr-wiki.de/wiki/index.php/ACPI_Wakeup vdr-wiki.de] {de} * [https://www.mythtv.org/wiki/ACPI_Wakeup mhythtv.org] {en} == NVRAM-Wakeup == * [sourceforge:nvram-wakeup:Projektseite] {en} * [http://www.vdr-wiki.de/wiki/index.php/NVRAM_WakeUp vdr-wiki.de] {de} * [http://www.vdr-wiki.de/wiki/index.php/C't-VDR_-_NVRAM_WakeUp C't-VDR - NVRAM WakeUp] {de} = Erweiterungen = == Andere Dienste == [:NFS: NFS] wird noch nicht auf aktive Verbindungen überwacht. Man kann sich behelfen, indem der Client-PC in die Clientliste (`CLIENTS=`) eingetragen wird. = Links = * [:Skripte:] {Übersicht} Übersichtsseite #tag: System, Shell, Server, Programmierung