What's the easiest way to have a script run at boot time in OS X?

12

8

I want a script (bash/zsh/ruby/...) to run at boot time in OS X. What's the most simple way to do this, without messing with xml/plist files, and preferably not needing to make a meta AppleScript.

John Bachir

Posted 2011-02-14T15:49:59.427

Reputation: 1 169

Answers

7

MacOS X uses Vixie cron, which has special meta tags for launching at reboot time. See the man page for the file format.

something like:

@reboot /path/to/script.sh

in your crontab would work. I'm not sure that this a better solution than launchd, you probably have more meta tools that look at launchd than cron.

Rich Homolka

Posted 2011-02-14T15:49:59.427

Reputation: 27 121

plist won't work for me - this works like a charm - thank you! :-D – BG Bruno – 2015-11-30T01:18:51.980

Honestly wasn't sure if the syntax would be the same on OS X. – NobleUplift – 2019-03-25T19:55:00.953

Love it, you proved me wrong :-) Although cron on OS X is not that great with logging by default (there was a topic on that just a few days ago). – Daniel Beck – 2011-02-14T16:16:02.213

this is perfect. – John Bachir – 2011-02-14T18:59:02.137

1

Note that cron, at, and so on are to some extent deprecated in OS X. I can't find an explicit statement of that in the various docs, nor do I know how aggressively deprecated they are, but launchd does seem generally preferred. See the launchd documentation for an introduction.

– Norman Gray – 2011-02-15T18:55:00.583

6

In case you change your opinion:

<?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.superuser.245713</string>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/script.sh</string>
    </array>
    <key>UserName</key>
    <string>someuser</string>
</dict>
</plist>

Store as com.superuser.245713.plist in /Library/LaunchAgents/ and make root:wheel the owner/group.

Daniel Beck

Posted 2011-02-14T15:49:59.427

Reputation: 98 421

1

It's arguably better to put it into /Library/LaunchAgents rather than /System/Library/LaunchDaemons since /System is OS-specific stuff and the one under /Library is used more for third party stuff. Also, LaunchDaemons "should contain items that will run as root, generally background processes" where as LaunchAgents "run as a user or in the context of userland". The source for those quotes is a great article on launchd that I consult for launchd questions.

– Doug Harris – 2011-02-14T16:19:32.427

@DougHarris Thanks for the suggestions! I have to admit I was just typing this ad-hoc -- while I usually test my solutions, I wasn't willing to restart my machine for this. – Daniel Beck – 2011-02-14T16:22:58.167

This is very nice, although I like the Vixie cron solution better :) – John Bachir – 2011-02-14T18:59:26.540

Actually, it should be put in /Library/LaunchDaemons. Agents only run inside a user session, i.e. they won't run (or more precisely, become eligible to run) until someone logs in, will run again every time someone logs in, and always run as the currently logged in user. Daemons run (/become eligible to run) at boot, and while they normally run as root, can be run as some other user with the UserName key. – Gordon Davisson – 2012-04-25T04:56:49.860

@DougHarris awwww dude your link is broken. 3 years later, but awwww just the same. – Randy L – 2014-03-24T20:48:44.523

2

@the0ther The web doesn't forget.

– Daniel Beck – 2014-03-24T21:23:54.533

2

There are also Login Hooks if you would prefer the script to run (as root) when a user logs in rather than when the machine is booted.

Deditos

Posted 2011-02-14T15:49:59.427

Reputation: 855