ubuntuusers.de

Auto OFF

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

Dieser Artikel ist größtenteils für alle Ubuntu-Versionen gültig.

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 eine neue Datei /etc/cron.d/checkshutdown erzeugt. Diese Datei wird in einem Editor[1] mit Root-Rechten[2] bearbeitet:

1
2
# 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 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]:

  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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/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.

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

ACPI-Wakeup

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 23. Juni 2022 01:01 von karteileiche erstellt.
Die folgenden Schlagworte wurden dem Artikel zugewiesen: Programmierung, Server, System, Shell