Let's explore fork and let me already say that fork only runs on Mac and Linux and I'll show a solution
for Windows too of course,
so let's simply see what fork does.
If I run my mongod instance with fork, like this and I can still pass dbpath and all the other options
if I want to
but if I run it like this, what happens is that I first of all get an error because one thing I need
to set here is the logpath and you'll see why in a second.
So I'll set my logpath, copy in the path to logs and then log.log and now what happens is this
starts my mongodb server as a child process and you can see it's now not blocking this window,
I can type other commands here again if I want to,
so I can type other commands here and the server is still running but now as a background process you
could say instead of a foreground process which blocks this window, it runs as a service put in other
words, a service in the background
and therefore I can run Mongo to connect to it because the server is up and running,
I can connect to it and I can work with it just as I did before.
That is also the reason why I had to pass my logpath, since it runs in the background,
it can't log errors or warnings to that front screen, it can't log it here into my terminal
instead it will write all the logs for the background server into my log file here.
This is simply how it works and how it runs and how I can run this as a background process if I want
to.
Now on Windows, the fork command is not available but on Windows, you can still start it as a service if
you checked that install as a service option during the installation which we did in this course.
So with that checked, you can start the mongodb service and you first of all need to make sure that
you quit all existing processes in all your command prompts.
You can start it by right clicking on the command prompt and running it as administrator
and then you can type net start mongodb and this will start up the mongodb server as a background
service.
Now the question of course is how can you quit such a running service and on Mac, you can easily do that
by connecting to that server with the Mongo shell and then switching to the admin database
and in there you can run db shutdown server,
so this method and this will shut down the server to which you are connected.
The exact same approach will work on Windows,
so this is one way of doing that.
On Windows you can also easily stop it by again opening a command prompt as administrator and running
net stop mongodb in there, written like this,
so this will also stop the service. And this is how you can use such a background service instead of a
foreground service,
what you use is up to you ultimately, up to your preference,
you got exactly the same service in both ways.
I just wanted to show the alternatives here.