I used to have a headless Linux LAN server that ran 24/7, i.e. even when I actually didn’t use it. Now, it powers itself off automatically as soon as it doesn’t detect any running workstations/notebooks (with dynamically assigned IP addresses) in the LAN.
Here’s how to do it:
In /usr/local/bin, create a new bash script named “shutdown_if_no_dhcp_client_in_lan” with the following content:
#!/bin/sh
# Shutdown the box if there aren't any (not ignored)
# DHCP clients in the LAN
# ignore the skype base station
ignored_macaddr="aa:bb:cc:dd:ee:ff"
# dhcp range starts at 192.168.1.$dhcpstart
dhcpstart=32
# dhcp range ends at 192.168.1.$dhcpend
dhcpend=46
for i in $(seq $dhcpstart 1 $dhcpend)
do
# returns 0, if ip reachable. returns 1, if not reachable
ping -w 1 -q 192.168.1.$i >/dev/null
if [ "$?" = "0" ]; then
# check whether this ip should be ignored
if [ "`ip neighbor|grep 192.168.1.$i|grep $ignored_macaddr|wc -l`" = "1" ]; then
echo "192.168.1.$i is reachable but ignored"
else
echo "192.168.1.$i is reachable and NOT ignored"
# exit the program without shutting down the box
exit;
fi
else
echo "192.168.1.$i is NOT reachable"
fi
done
# we didn't find any (not ignored) DHCP client in the LAN
# we can thus shutdown this box
echo "warning the users and shutting down the box in 5 minutes"
shutdown -h -P +5 Please save your work now!
(Adjust the script to your LAN environment)
In /etc/crontabs, add:
*/20 * * * * root /usr/local/bin/shutdown_if_no_dhcp_client_in_lan >/dev/null 2>&1
Now the LAN server will shutdown automatically after a while when no workstations/notebooks are running anymore. Note that this can happen 5 to 25 minutes after the last activity and isn’t fault-tolerant at all. The script can easily be improved however.