How to Automatically Launch NUT Client at Boot on MacOS
I'm trying to run NUT (Network UPS Tools) on my Mac (MacOS 10.9.5) so that my Mac mini server will shutdown cleanly in the event of a power outage and the UPS battery runs out. I've been following these instructions:
https://community.netgear.com/t5/New-to-ReadyNAS/NUT-on-OSX-10-6-Sharing-a-UPS-with-ReadyNAS-and-Computers/td-p/661293
I've got the NUT server running on my Synology NAS and the NUT client installed on my Mac mini (via Fink) and can manually launch it by doing:
sudo upsmon
I pulled the power on the UPS and saw power events written to the Terminal and to the Console. I haven't let it drain all the way down yet, but it appears to be working so far.
My issue is that I've been unable to get it automatically launch with OS X. I've tried running a script that was included with the instructions:
#!/bin/sh
##
# UPS Monitor script
# Using Network UPS Tools executables
##
. /etc/rc.common
FINK_BIN="/sw/sbin"
StartService ()
{
ConsoleMessage "Starting NUT (UPS Service)"
if [ -n "`ps acxw | grep -i "upsmon" | awk {'print $1'}`" ]; then
ConsoleMessage -f "NUT (UPS Service)"
echo "Failed to start NUT (UPS Service): It is already running."
exit 0
fi
${FINK_BIN}/upsmon &
if [ -n "`ps acxw | grep -i "upsmon" | awk {'print $1'}`" ]; then
ConsoleMessage -s "NUT (UPS Service)"
echo "NUT (UPS Service) successfully started."
exit 0
else
ConsoleMessage -f "NUT (UPS Service)"
echo "Failed to start NUT (UPS Service): Either the application has been deleted or it has no execution right."
exit 0
fi
}
StopService ()
{
ConsoleMessage "Stopping NUT (UPS Service)"
if [ -z "`ps acxw | grep -i "upsmon" | awk {'print $1'}`" ]; then
ConsoleMessage -s "NUT (UPS Service)"
echo "Failed to stop NUT (UPS Service): It is not running."
exit 0
fi
${FINK_BIN}/upsmon -c stop
if [ -n "`ps acxw | grep -i "upsmon" | awk {'print $1'}`" ]; then
ConsoleMessage -s "NUT (UPS Service)"
echo "Failed to stop NUT (UPS Service) out of unknown reason."
exit 0
else
ConsoleMessage -s "NUT (UPS Service)"
echo "NUT (UPS Service) successfully stopped."
exit 0
fi
}
StopServiceForcefully()
{
ConsoleMessage "Stopping NUT (UPS Service)"
if [ -z "`ps acx | grep -i "upsmon" | awk {'print $1'}`" ]; then
echo "Failed to stop NUT (UPS Service): It is not running."
else
kill -kill `ps acxw | grep -i "upsmon" | awk {'print $1'}`
if [ -n "`ps acxw | grep -i "upsmon" | awk {'print $1'}`" ]; then
echo "Failed to stop NUT (UPS Service) out of unknown reason."
else
echo "NUT (UPS Service) successfully stopped."
fi
fi
}
RestartService()
{
ConsoleMessage "Trying to restart NUT (UPS Service)."
StopService
StartService
exit 0
}
RunService "$1"
However, I get this error in Terminal:
line 75: $1: unbound variable
Can this script be fixed? Is it possible to just create a new script that will just launch upsmon and keep it simple. I can't figure it out; I've tried a few things to do just that but couldn't get it to work.
I came across this question while trying to remember how I did it myself, but I think I re-found the solution. I followed the directions in this mailing list post; it is a few years old but it is still working for me in macOS Sierra.
If that post becomes unavailable, here's a copy of the key info in it. Basically, once you have your upsmon
configuration set up the way you want it, you just have to create a .plist file (simply a text file with the .plist extension) and put it in the right directory, then restart. I used the verbatim text from the post for the .plist file and it is still going strong for me.
Specifically, you can create a .plist file with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.networkupstools.upsmon</string>
<key>OnDemand</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/opt/local/sbin/upsmon</string>
<string>-D</string>
</array>
</dict>
</plist>
Then save it with a name like org.networkupstools.upsmon.plist
in /Library/LaunchDaemons
and it should start the service automatically on boot from that point onward.