For the usage of a local backup host, I defined the following scenario, which could also be used for a printer server or any other type of host, which shouldn’t run 24/7, but:
- The host should be able to be woken up manually by a special signal on the LAN, i.e., it should be capable of WOL. (This is only a hardware issue)
- If it is woken up, it should run at least for 30 minutes before trying to shutdown again.
- It shouldn’t shut down while certain processes are running, such as a backup, i.e., it should be capable of a shutdown-lock. If the lock is removed, it might shutdown.
- The host shouldn’t shut down as long as a certain client, e.g. a backup client, is (still) up and running (pingable)
- If the criteria above aren’t hurt, the host should finally shut down.
And here’s the shell-script which implements the above. It’s enough to have it run every five minutes. The “echoes” are only interesting for debugging purposes.
#!/bin/bash
lockfile=/etc/lock_shutdown
upclient=fourier
min_upminutes=30
# If shutdown locked, keep running
if [ -f $lockfile ]; then
echo “Locked”
exit 0
fi
# If client is still there, keep running
if ping -t10 -c1 $upclient &>/dev/null; then
echo “$upclient still there”
exit 0
fi
# If we are up long enough ...
cur_secs=$(cut -d. -f1 /proc/uptime)
if [ $cur_secs -lt $[$min_upminutes*60] ]; then
echo “Not up long enough”
exit 0
fi
# ... then shut down!
echo “Shutting down”
/sbin/shutdown -h 1 &