Run script as root at startup - macOS 10.12 Sierra
I've tried everything and can't seem to get my script to run at startup. Modifying the .bashrc, .bash_profile, etc that route got me no where. Then I tried cron jobs, no luck. Then I've been pulling my hair trying to use the launchd function. I've made a launch daemon, which is allegedly the root functions, but that's no luck either. As a last try, I even tried Automator with no luck.
The file I want to run as root is: "/Users/CURRENT_USER/Dev/my_script.sh"
Here is what I have in my LaunchDaemons folder:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.startup.script</string>
<key>LaunchOnlyOnce</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>/Users/CURRENT_USER/Dev/my_script.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>root</string>
</dict>
</plist>
A ~/Library/LaunchDaemons doesn't exist. A launch daemon in /Library/LaunchDaemons is already executed with root privileges so you may omit the UserName part.
So put your shell script my_script.sh in /usr/local/bin/ or in /usr/bin/ (adjust the path in the ProgramArguments array if you use the latter), make it executable with sudo chmod +x ...
and owned by root:wheel with sudo chown root:wheel ...
. /usr/bin is recommended if brew is installed.
Modify your current plist to:
<?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.startup.script</string>
<key>LaunchOnlyOnce</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>/usr/local/bin/my_script.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Save the launch daemon as com.startup.script.plist in /Library/LaunchDaemons/ and owned by root:wheel.
Launch it with:
sudo launchctl load /Library/LaunchDaemons/com.startup.script.plist