如何在 MacOS 上启动时自动启动 NUT 客户端

如何在 MacOS 上启动时自动启动 NUT 客户端

我正在尝试在 Mac(MacOS 10.9.5)上运行 NUT(网络 UPS 工具),以便在断电和 UPS 电池耗尽时,我的 Mac mini 服务器能够干净地关闭。我一直遵循以下说明:

https://community.netgear.com/t5/New-to-ReadyNAS/NUT-on-OSX-10-6-Sharing-a-UPS-with-ReadyNAS-and-Computers/td-p/661293

我已在 Synology NAS 上运行 NUT 服务器,并在 Mac mini 上安装了 NUT 客户端(通过 Fink),可以通过执行以下操作手动启动它:

sudo upsmon

我关闭了 UPS 电源,看到电源事件写入终端和控制台。我还没有让它完全耗尽电量,但到目前为止它似乎还在运行。

我的问题是我无法让它随 OS X 自动启动。我尝试运行说明中包含的脚本:

#!/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"

但是,我在终端中收到此错误:

line 75: $1: unbound variable

这个脚本能修复吗?是否可以创建一个新脚本,只启动 upsmon 并保持简单。我搞不懂;我试过几种方法,但都无法让它工作。

答案1

我在回忆自己是怎么做的时遇到了这个问题,但我想我重新找到了解决方案。我按照此邮件列表帖子;它已经有几年的历史了,但它仍然可以在 macOS Sierra 中为我工作。

如果该帖子不可用,以下是其中关键信息的副本。基本上,一旦您按照upsmon自己想要的方式设置了配置,您只需创建一个 .plist 文件(只需一个带有 .plist 扩展名的文本文件)并将其放在正确的目录中,然后重新启动。我使用了帖子中的逐字文本作为 .plist 文件,它对我来说仍然很强大。

具体来说,您可以创建一个包含以下内容的 .plist 文件:

<?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>

org.networkupstools.upsmon.plist然后使用类似的名称保存它/Library/LaunchDaemons,从那时起它应该在启动时自动启动该服务。

相关内容