Sean256
Sean256

Reputation: 3099

LaunchAgent run bash script as root

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

The LaunchAgent is in /Library/LaunchAgents

How can I make locationChanger.sh run as root? The script throws erros saying it needs to be root. If I add sudo in front of the lines that need root it throws an error stating a password is needed.

The lines in the bash script that need root are "route add" and route delete"

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

Upvotes: 0

Views: 2782

Answers (1)

Mateusz Szlosek
Mateusz Szlosek

Reputation: 1225

Explanation on superuser: https://superuser.com/questions/36087/how-do-i-run-a-launchd-command-as-root

Put Your .plist to /Library/LaunchDaemons and chown it as root.

Upvotes: 1

Related Questions