Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was not sure if this was for StackOverflow or here, I settled for here. I was wondering which file I should place this bash command in so it will be run on startup.

# Start the MongoDB server
/Applications/MongoDB/bin/mongod --dbpath /usr/local/mongo/data --fork --logpath /usr/local/mongo/log

I have been scouring the net and think it is between ~/.bashrc, ~/profile, /etc/bashrc, /etc/profile or ~/.bash_profile. Although I have tried these and they seem to run on terminal startup not Mac startup. Am I missing a file here?

Thanks for any help you can give.

share|improve this question

4 Answers 4

up vote 19 down vote accepted

Officially none of these. The Apple suggested way is to use launchd. A Gui to set this up is lingon

As for the files you mention the ones in the home directory ~/.bashrc, ~/profile, ~/.bash_profile are only started when you login via a terminal.

EDIT: The ones in /etc are run by the shell starting for all users before the one in home directory. bash manual

The Unix startup script involved /etc/rc* but for OSX just use the launchd stuff

share|improve this answer
    
So if my command is inserted in either of the files in /etc it should be run on bootup? Does it matter what one it is in? –  Wolfy87 Jan 6 '11 at 11:45
    
/etc/bashrc and so on are run when you start a shell, just like ~/.bashrc - it's just that the former will be run whenever any user starts a shell, rather than just your user. –  Scott Jan 6 '11 at 12:07
    
@Scott is correct I have corrected my answer –  Mark Jan 6 '11 at 12:14
1  
Okay, but I just can't work out how to use launchd, I tried making a plist file for my program but I have no idea how to run it or how to get it to run on boot. –  Wolfy87 Jan 6 '11 at 12:27

To run a command on start up on OS X, you need to use launchd.

If you don't want to use Lingon, you need to create a launchd Property List. This is an XML file, so you can do it with your favourite text editor or alternatively you can use the Property List Editor that's installed with the Mac OS X Dev Tools. Create the following:

<?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>some.meaningful.name</string> <!-- org.mongodb.mongodb perhaps? -->

    <key>OnDemand</key>
    <false/>

    <key>UserName</key>
    <string>anAppropriateUser</string>

    <key>GroupName</key>
    <string>anAppropriateGroup</string>

    <key>ProgramArguments</key>
    <array>
            <string>/Applications/MongoDB/bin/mongod</string>
            <string>--dbpath</string>
            <string>/usr/local/mongo/data</string>
            <string>--fork</string>
            <string>--logpath</string>
            <string>/usr/local/mongo/log</string>
    </array>
</dict>
</plist>

Save this in /Library/LaunchAgents/some.meaningful.name.plist (you will need an administrator account and/or sudo), then open a terminal and do:

sudo launchctl load /Library/LaunchAgents/some.meaningful.name.plist

This will cause launchd to load the item which will cause it to start MongoDB on boot. As a bonus, launchd will monitor it and, if it exits for any reason, it will be re-started. To get rid of the item simply replace load in the above command with unload.

share|improve this answer
    
This page (developer.apple.com/library/mac/documentation/Darwin/Reference/…) lists a lot of things something launched by launchd should not do. If I want to run an arbitrary command at startup I may not have control of and the command may wind up doing those things. In addition, I may not want launchd to restart and item that stops because it's a one-off or handles re-launching on its own. What should I do in these cases? –  Michael Nov 8 '13 at 22:17
    
@Scott, thank you. This is the most helpful answer I found on the internet. –  geotavros Feb 10 at 15:04

Another simple solution from Stack Overflow: You can:

  • Start Automator.app;
  • Select "Application";
  • Click "Show library" in the toolbar (if hidden);
  • Add "Run shell script" (from the Actions/Utilities);
  • Copy-and-paste your script into the window;
  • Test it;
  • Save it somewhere: a file called your_name.app will be created);
  • Go to System Preferences → Accounts → Login items;
  • Add this newly-created app;

Log off, log back in, and you should be done. ;)

share|improve this answer
1  
Tested and works and it does not require launchd cumbersome plist editing nor paid Lingon app. –  Lukasz Oct 2 '12 at 8:13

You will have to look at how launchd and launchctl work on MacOS. You could start by reading the man pages for both the commands. You could then look inside /Library/LaunchAgents/ and /Library/LaunchDaemons/ for examples of how to set up applications to load at different times through the launchctl interface.

Here's an example I found on Stack Overflow that might help you further.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.