I would like to run mongod in the background as an always present sort of thing. What would be the best way to do this? Kind of like the way I can run MySQL on startup and it's just always there running in the background. Maybe it's just some bash scripts, but it would be nice to hear if there is a better way. If it is just bash - what would that look like? Thanks.
-
1`npm install pm2 -g` + `pm2 start mongod` + `pm2 save` is a nice cross platform way to to keep any background process running across sessions. – Jthorpe May 08 '17 at 18:05
6 Answers
The MongoDB daemon (mongod) has a command-line option to run the server in the background...
--fork
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
![](../../users/profiles/49377.webp)
- 1,069
- 10
- 9
-
-
-
./bin/mongod --fork --logpath=logs/mongod.log --dbpath=data/db worked for me – Selvakumar Esra Mar 26 '17 at 10:13
-
If you don't want create an extra log file, there is also the option `--syslog` in wich everything is just written into system log. – Markus Graf Mar 30 '17 at 09:21
-
@Jesse Webb. And will it keep running after I logout, for example, the ssh connection? – Miguel Dec 08 '17 at 15:10
-
@miguel If you run it as a service, yes it will continue running after logout. I'm not sure though what happens if you run the command directly. – Jesse Webb Dec 08 '17 at 15:58
-
Hill, another question. What if I want to run it from a configuration file? Will it be ok to just use ``mongod --fork --config /etc/mongod.conf ``? – Miguel May 05 '18 at 09:21
-
-
so the basics of creating a service are as simple as putting the above command into an /etc/init.d ? reading the linked tutorial though I would need to write more in order to have start/stop etc. For whatever reason my mongo install doesn't have a service to manage it. – dcsan Nov 13 '18 at 04:02
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.
![](../../users/profiles/283889.webp)
- 1,213
- 3
- 15
- 22
![](../../users/profiles/10126.webp)
- 3,293
- 8
- 32
- 35
-
1updated: `mkdir -p ~/Library/LaunchAgents` `cp /usr/local/Cellar/mongodb/3.2.10/homebrew.mxcl.mongodb.plist ~/Library/LaunchAgents` `launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist` – helsont Oct 07 '16 at 17:09
Classically, to run in the background, just start with
nohup some_command -to launch &
So that is nohup &, put the command you want to run in the background between nohup and &. It will run as you, it will keep running after you close the terminal or log out. You could start it as another user via sudo. To make it run on start, add it to the init scripts replacement (don't know what they call it). It will write any output to a file where you started it called nohup.out
![](../../users/profiles/8668.webp)
- 1,683
- 1
- 11
- 19
according to mongodb you can do that by just following these steps. check this out: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/#run-mongodb-community-edition
![](../../users/profiles/979752.webp)
- 101
The best way to get a program to run at startup on OS X is to create a LaunchDaemon (see Apple's docs, and take a look at some of the Apple-supplied daemons in /System/Library/LaunchDaemons) and install it in /Library/LaunchDaemons. Lingon can help you create the .plist file.
![](../../users/profiles/6621.webp)
- 11,036
- 3
- 27
- 33