How can I run mongod in the background on unix (mac osx)?

Solution 1:

The MongoDB daemon (mongod) has a command-line option to run the server in the background...


This command-line option requires that you also specify a file to log messages to (since it can not use the current console). An example of this command looks like:

mongod --fork --logpath /var/log/mongod.log

You could put this into an /etc/init.d/mongod bash script file. And then to have the service run at startup, create the standard symbolic links (S## & K##) inside of /etc/rc#.d/. Here is a tutorial that explains this process in more detail. Scroll down to the section titled "Init Script Activation". This also has the added benefit of being able to execute commands like...

service mongod status
service mongod start
service mongod stop

Solution 2:

Since you are on a Mac, you should use Homebrew to install MongoDB (you will need to install Homebrew if you haven't yet):

brew install mongodb

Let's say the version that was installed is named 2.0.6-x86_64. Then add the LaunchAgent plist file that it generates for you:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mongodb/2.0.6-x86_64/homebrew.mxcl.mongodb.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

This will launch mongod in the background now and every time you restart your computer.

Solution 3:

try running the following command:

mongod --fork --syslog