How to run a command at startup I want to automatically start my MongoDB Server at startup. The command for it is

mongod --dbpath="/Users/prateek/Desktop/Mongo Database"


How do I configure this in Mac OSX El Capitan?

Note - I have installed MongoDB using Homebrew
Create a file named org.mongod.user.plist in ~/Library/LaunchAgents with the content:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;org.mongod.user&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;mongod&lt;/string&gt;
        &lt;string&gt;--dbpath=/Users/prateek/Desktop/Mongo Database&lt;/string&gt;
    &lt;/array&gt;
    &lt;key&gt;RunAtLoad&lt;/key&gt;
    &lt;true/&gt;
&lt;/dict&gt;
&lt;/plist&gt;


This requires that mongod is in your path.

If mongod is not in your path, use the whole path in the first string of the ProgramArguments array (e.g. /usr/local/Cellar/mongodb/3.2.1/bin/mongod)

If you need stdout or stderr (which can be helpful if something fails) add the following in the dict section of the plist:

    &lt;key&gt;StandardErrorPath&lt;/key&gt;
    &lt;string&gt;/tmp/org.mongod.user.stderr&lt;/string&gt;
    &lt;key&gt;StandardOutPath&lt;/key&gt;
    &lt;string&gt;/tmp/org.mongod.user.stdout&lt;/string&gt;


Load and start mongod with:

launchctl load ~/Library/LaunchAgents/org.mongod.user.plist
launchctl start org.mongod.user




To connect to your MongoDB database enter:

mongo localhost


Again this requires that mongo is in your path. Otherwise use the complete path (e.g. /usr/local/Cellar/mongodb/3.2.1/bin/mongo).