how to get shell scripts to run at startup on Yosemite
One way of doing it would be assigning the script a launchd service:
Create the shell script as usual. Then you can make a launchd service to run it at startup. Those are located at /Library/LaunchDaemons
. These are in the XML property list format. Create another and populate it with something like this:
<?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.example.app</string>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>/path/to/script</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
Change com.example.app
, /bin/sh
and /path/to/script
as required.
The script will then run while the system is booting. If it runs too early, you can either write the script to try and do what it needs to do until it succeeds, or have it exit with a non-zero error code and add this to the property list before the </dict>
line:
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
For more on OS X launch daemons and services, I suggest looking here for a nice quick-reference on making them, or here for a more comprehensive reference on what launchd can do.
Some time ago I used cron to do just that. You can make an entry like this
@reboot /path/to/my/script
More info here
Instead of the first five fields, one of eight special strings may appear:
string meaning
------ -------@reboot ------@reboot
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".