How do I run mongod in the background?

2,315 views
Skip to first unread message

Charlie

unread,
Jul 30, 2009, 8:00:38 PM7/30/09
to mongodb-user
I'm running OS X (32-bit), using the following:

./mongod -f ./mongod.ini

It starts up, listens on the correct ports and runs, but it never
returns back to the command line. If I close the Terminal window
mongod stops running.

How do I run it in background?

Thanks

Charlie

unread,
Jul 30, 2009, 8:18:30 PM7/30/09
to mongodb-user
PS I've also tried running with the & at the end (./mongod -f ./
mongod.ini &) but this doesn't work.

Kristina Chodorow

unread,
Jul 30, 2009, 8:44:23 PM7/30/09
to mongod...@googlegroups.com
The easiest way is to run it in the background:

./mongod -f ./mongod.ini >> logfile&

If you actually want to fork the process, you could look into using the start-stop-daemon.  However, the start-stop-daemon won't let you redirect output (as far as I've been able to figure out) so you'll have to write a shell script with the line above (minus the ampersand) and run something like:

start-stop-daemon --start --background --quiet /home/user/startmongo #assuming you called your shell script startmongo

Run "man start-stop-daemon" to figure out which options you want to use.

Martin Streicher

unread,
Jul 31, 2009, 8:11:30 AM7/31/09
to mongod...@googlegroups.com

Check you shell's options for something akin to nohup, which keeps background jobs alive. Another option is the nohup command. And even better is screen. Start screen, run mongod (no need to background it) and then detach. You can reattach later using screen -r. I have run background jobs for weeks under screen. 

You may need to look at writing an init script and using start-stop-daemon to start mongo at boot time. You can also use @boot in cron to kick it off. 

Charlie

unread,
Jul 31, 2009, 8:25:41 AM7/31/09
to mongodb-user
THanks Kristina; that's worked (I was missing the logfile definition
to hold output).

I created a couple of shell .command files to quickly start and stop
the daemon from OS X, though I needed to use the full path when
referencing files:

startMongod.command:
/mongodb/mongod -f /mongodb/bin/mongod.ini >> /mongodb/bin/mongod.log
&

stopMongod.command (thanks to another post in this group):
pid=`ps -o pid,command -ax | grep mongod | awk '!/awk/ && !/grep/
{print $1}'`;
if [ "${pid}" != "" ]; then
kill -2 ${pid};
fi


On Jul 31, 1:44 am, Kristina Chodorow <krist...@10gen.com> wrote:
> The easiest way is to run it in the background:
>
> ./mongod -f ./mongod.ini >> logfile&
>
> If you actually want to fork the process, you could look into using the
> start-stop-daemon.  However, the start-stop-daemon won't let you redirect
> output (as far as I've been able to figure out) so you'll have to write a
> shell script with the line above (minus the ampersand) and run something
> like:
>
> start-stop-daemon --start --background --quiet /home/user/startmongo
> #assuming you called your shell script startmongo
>
> Run "man start-stop-daemon" to figure out which options you want to use.
>
Reply all
Reply to author
Forward
0 new messages