0

This is what I have so far. This script would run on a Network Services host that does things like DHCP etc... and in this instance should be waking and sleeping computers for backup.

#!/bin/bash

## This script exists to wake up the Digital Audio Workstation (DAW) and the NAS on which its session files and other data exists so that the DAW may run its own rsync localbackup script.
## The script pauses between WOL packets because we want to give the NAS time to wake up so that the DAW can mount one of it's shares when it wakes up.
## This script also shuts down the hosts if the condition is met that no human users are logged into the DAW.
## This script is called daily by cron in <cite path and name of cron file here>

## wake the hosts
etherwake -b -i eth0 11:22:33:44:55:66
sleep 10m
etherwake -b -i eth0 22:33:44:55:66:77

## wait for 15 minutes for the backup to complete then check rsync is nolonger running and shutdown the hosts if no users are logged in
sleep 15m
while [ tail rsync on remote host (DAW) for exit code 0 ] && [ -n "$(who)" ] ;do
sleep 1m
done
<send shutdown signal to remote host>
jerk
  • 1
  • 2

1 Answers1

0

Testing for the absence of users while connected via SSH is never going to indicate that there are no users because you are logged in when you run it. The simpler solution would be to do the shutdown in the same script doing the rsync. Adding something to the end similar to:

while true; do
  [ -z "$(who)" ] && /sbin/poweroff
done

If the machine doesn't need to be powered on when nobody is logged in, just always power it off after the backup. If you don't want it to shut down during the day so people don't waste time booting it, check the time with date:

while true; do
  [ -z "$(who)" ] && [ "$(date +%k)" -lt 5 ] && /sbin/poweroff
done

The +%k option for date returns the current hour from 0-24. The above example will shut down if it is the backup completes between 12:00 AM and 5:00 AM.

Steve F
  • 321
  • 1
  • 2
  • 9