0
<?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>com.nords.locationchanger</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/locationChanger.sh</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/SystemConfiguration</string>
    </array>
</dict>
</plist>

LaunchAgent 位于 /Library/LaunchAgents

如何让 locationChanger.sh 以 root 身份运行?该脚本抛出错误,说它需要是根。如果我在需要 root 的行前面添加 sudo,它会抛出一个错误,指出需要密码。

bash 脚本中需要 root 的行是“路由添加”和“路由删除”

#!/bin/bash

exec 2> >(logger -t NETWORKSCRIPT)

location=$(networksetup -getcurrentlocation)
internalName="Internal"

if [ $location == $internalName ];
then
    echo "Internal Network!"
    logger -i "NETWORKSCRIPT CHANGE: Internal network, setting up custom routs and proxy stuff"
    git config --global http.proxy http://webproxysea.██████.net:8181
    route add -host 54.80.220.236 192.168.15.1
else
    echo "External Network!"
    logger -i "NETWORKSCRIPT CHANGE: $location network, removing custom stuff!"
    git config --global --unset http.proxy
    route delete -host 54.80.220.236 192.168.15.1
fi
4

1 回答 1

1

超级用户说明:https ://superuser.com/questions/36087/how-do-i-run-a-launchd-command-as-root

把你.plist/Library/LaunchDaemonschown它作为根。

于 2014-04-08T19:10:13.863 回答