25

There is a directory in System/Library for startup items I presume: StartupItems

I need to have a shell script that will configure ethernet interface at start up, with local network address and subnet mask. I need to do this because the network preferences for configuring the ethernet interface will NOT set it using manual settings. THIS appears to be a serious bug, or my installation is bad.

I was given the advice to use networksetup from the command line but that will not configure the ethernet interface in such a way that the configurations will be there on reboot.

The script needs to do its work with root privileges, or with sudo, but I am assuming that if it uses sudo, there will be a password dialog presented at boot time when the boot process gets to that script.

(I have had enough experience with FreeBsd and Linux to find my way around in the terminal, but not necessarily as it applies to Mac OSX)

3 Answers 3

20

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.

5
  • What do you call this plist document? And does the /path/to/script == /System/Library/StartupItems/script?
    – JeffK
    Commented Nov 14, 2014 at 17:31
  • 1
    For the first question: The plist document's name generally uses what the Label key in the property list is set to (in this case, "com.example.app") with a .plist extension.
    – andonuts
    Commented Nov 16, 2014 at 14:53
  • 1
    For the second question: That is correct, but the script should be moved elsewhere.
    – andonuts
    Commented Nov 16, 2014 at 14:55
  • Your plist is malformed. You need a Key: <key>Program</key> with the value <string>/bin/sh</string> and then <key>ProgramArguments</key> should just have the value <string>/path/to/script</string>
    – Jon Brooks
    Commented Oct 6, 2015 at 20:21
  • Actually, mine is wrong too: <key>ProgramArguments</key> needs to be an array..
    – Jon Brooks
    Commented Oct 6, 2015 at 20:43
19

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 * * * *".
8
  • 1
    Very handy to know!
    – geotheory
    Commented Mar 23, 2015 at 17:21
  • 7
    cron is not the right way to do this on OS X these days. Use LaunchAgent or LaunchDeamon instead (launchd). For more info: developer.apple.com/library/mac/documentation/MacOSX/Conceptual/… Although it is still supported, cron is not a recommended solution. It has been deprecated in favor of launchd. Commented Jul 22, 2015 at 21:23
  • 5
    cron is a lot easier to configure than launchd, though. Just compare the two answers in this thread to see what I mean. cron takes a single line in a single file, where launchd needs a script to describe the script you want run at boot.
    – Dannid
    Commented Sep 22, 2015 at 18:02
  • @Dannid Kron is not sensitive to run duplication, queue buffering, and others. Though viable in this case, it's not always a skeleton key, nor is it an idiomatic solution. Commented Oct 24, 2015 at 16:01
  • Crontab is way better. It's quick and dirty and long established in the Unix world. I totally dig this @reboot keyword.
    – macetw
    Commented Mar 15, 2017 at 2:11
-3

Take a look at login hook script.

https://wiki.afp548.com/index.php/Login/Logout_Hooks_in_OS_X

1
  • 1
    A login hook is initiated by the loginwindow.app which is not the same as running a script at startup.
    – fd0
    Commented Oct 8, 2015 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .