OS X: Add rc.local and Launchd entry for it

root's picture

Prior to OS X 10.10 Yosemite, you were able to start rc.local script via rc..
That is gone and you have to use launchd.

This tutorial assumes you have already your rc.local script in (example) /etc/rc.local and it is executable and properly populated with your desired commands.
So, without writing too much, below is my example of launchd script:

mini:~ osx$ cat /Library/LaunchDaemons/local.gz.startup.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>local.gz.startup</string>
    <key>Disabled</key>
        <false/>
    <key>UserName</key>
        <string>root</string>
    <key>RunAtLoad</key>
        <true/>
    <key>KeepAlive</key>
        <false/>
    <key>LaunchOnlyOnce</key>
        <true/>
    <key>ProgramArguments</key>
        <array>
                <string>/etc/rc.local</string>
        </array>
</dict>
</plist>

Now, your /etc/rc.local will be executed each time your computer starts. The field RunAtLoad is set to true and is actually telling the launchd to run the script /etc/rc.local at load.

If you want to start (reload) the script manually, you can execute the following:
sudo launchctl load -w /Library/LaunchDaemons/local.gz.startup.plist

And, as it is very clear for almost everybody, you only need this plist entry to have /etc/rc.local executed on boot. Once the system is up and running, you can always directly execute the script without the help of launchd.

What follows, was added just for you to understand a bit how launchd is working in this particular situation. If you are satisfied with your care, you can stop here :)
If you would prefer the script not to be executed on boot (which is not the point of this tutorial but let's...), you will have to set RunAtLoad to false and in this case you will have to run the following command to execute /etc/rc.local:
sudo launchctl start /Library/LaunchDaemons/local.gz.startup.plist
or, of course the classic:
sudo /etc/rc.local

An interesting idea is in the link below, which allows you to add commands also when the machine stops but it is not quite the same. I would stick for simple things with simple solutions. You would use the script below for more serious purposes, rather than a replacement of rc.local. Enjoy!
https://github.com/freedev/macosx-script-boot-shutdown

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content