ubuntuusers.de

ubuntuusers.deWikiSkripteAuto OFF

Auto OFF

Dieser Artikel wurde für die folgenden Ubuntu-Versionen getestet:

Zum Verständnis dieses Artikels sind folgende Seiten hilfreich:

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 oder zeitgesteuert geweckt.

Vorbereitungen

Cron Job

Ein Cron Job ruft das Skript alle 20 Minuten auf. Dazu wird in /etc/cron.d eine neue Datei checkshutdown erzeugt. Diese Datei wird in einem Editor[4] mit Root-Rechten[2] bearbeitet:

# Auto shutdown
*/20 * * * * root /usr/local/sbin/checkshutdown.sh

Skript

Das vom 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 Skript checkshutdown.sh wird in einen Editor[4] kopiert und mit Root-Rechten[2] in /usr/local/sbin/ gespeichert, außerdem muss es ausführbar gemacht werden[3].

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/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
}

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

	# 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
		        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 autoshutdown.conf wird in einen Editor[4] erstellt und mit Root-Rechten[2] in /etc abgespeichert.

Hier kann man seine Programme, Dämonen, Sambanetzwerke und Client-Rechner eintragen.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 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"

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.

  • Umbennenen 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, bei älterer Hardware kommt man eventuell mit NVRAM-Wakeup zum Ziel.

  • Weckdienst {de} auf Linux.magazin.de 2004/08 (ACPI Teil ist veraltet und funktioniert jetzt anders)

  • Wakeup {de}, Kategorie auf vdr-wiki.de

ACPI

Eine genaue Anleitung zu ACPI ist geplant. In der Zwischenzeit helfen einige externe Links.

NVRAM-Wakeup

Erweiterungen

Andere Dienste

NFS wird noch nicht auf aktive Verbindungen überwacht. Man kann sich behelfen, indem der Client-PC in die Clientliste (CLIENTS=) eingetragen wird.

Diese Revision wurde am 30. April 2011 20:07 von frustschieber erstellt.
Die folgenden Schlagworte wurden dem Artikel zugewiesen: Programmierung, Netzwerk, Server, System, Ubuntu, Shell

Passwort vergessen?