1 00:00:02,280 --> 00:00:09,360 Let's explore fork and let me already say that fork only runs on Mac and Linux and I'll show a solution 2 00:00:09,360 --> 00:00:11,240 for Windows too of course, 3 00:00:11,310 --> 00:00:14,000 so let's simply see what fork does. 4 00:00:14,400 --> 00:00:22,950 If I run my mongod instance with fork, like this and I can still pass dbpath and all the other options 5 00:00:22,950 --> 00:00:23,640 if I want to 6 00:00:23,760 --> 00:00:28,980 but if I run it like this, what happens is that I first of all get an error because one thing I need 7 00:00:28,980 --> 00:00:33,440 to set here is the logpath and you'll see why in a second. 8 00:00:33,480 --> 00:00:40,950 So I'll set my logpath, copy in the path to logs and then log.log and now what happens is this 9 00:00:40,950 --> 00:00:47,470 starts my mongodb server as a child process and you can see it's now not blocking this window, 10 00:00:47,610 --> 00:00:50,180 I can type other commands here again if I want to, 11 00:00:50,430 --> 00:00:55,890 so I can type other commands here and the server is still running but now as a background process you 12 00:00:55,890 --> 00:01:02,190 could say instead of a foreground process which blocks this window, it runs as a service put in other 13 00:01:02,190 --> 00:01:04,480 words, a service in the background 14 00:01:04,590 --> 00:01:09,350 and therefore I can run Mongo to connect to it because the server is up and running, 15 00:01:09,390 --> 00:01:14,570 I can connect to it and I can work with it just as I did before. 16 00:01:14,580 --> 00:01:19,190 That is also the reason why I had to pass my logpath, since it runs in the background, 17 00:01:19,200 --> 00:01:25,660 it can't log errors or warnings to that front screen, it can't log it here into my terminal 18 00:01:25,770 --> 00:01:30,970 instead it will write all the logs for the background server into my log file here. 19 00:01:31,200 --> 00:01:37,530 This is simply how it works and how it runs and how I can run this as a background process if I want 20 00:01:37,530 --> 00:01:38,360 to. 21 00:01:38,460 --> 00:01:45,300 Now on Windows, the fork command is not available but on Windows, you can still start it as a service if 22 00:01:45,300 --> 00:01:51,620 you checked that install as a service option during the installation which we did in this course. 23 00:01:51,630 --> 00:01:57,960 So with that checked, you can start the mongodb service and you first of all need to make sure that 24 00:01:57,960 --> 00:02:01,760 you quit all existing processes in all your command prompts. 25 00:02:02,070 --> 00:02:06,800 You can start it by right clicking on the command prompt and running it as administrator 26 00:02:06,960 --> 00:02:14,720 and then you can type net start mongodb and this will start up the mongodb server as a background 27 00:02:14,720 --> 00:02:16,710 service. 28 00:02:16,710 --> 00:02:22,140 Now the question of course is how can you quit such a running service and on Mac, you can easily do that 29 00:02:22,440 --> 00:02:28,090 by connecting to that server with the Mongo shell and then switching to the admin database 30 00:02:28,200 --> 00:02:30,950 and in there you can run db shutdown server, 31 00:02:30,960 --> 00:02:35,370 so this method and this will shut down the server to which you are connected. 32 00:02:35,490 --> 00:02:38,410 The exact same approach will work on Windows, 33 00:02:38,430 --> 00:02:40,350 so this is one way of doing that. 34 00:02:40,410 --> 00:02:47,070 On Windows you can also easily stop it by again opening a command prompt as administrator and running 35 00:02:47,070 --> 00:02:51,020 net stop mongodb in there, written like this, 36 00:02:51,120 --> 00:02:56,640 so this will also stop the service. And this is how you can use such a background service instead of a 37 00:02:56,640 --> 00:02:57,880 foreground service, 38 00:02:58,050 --> 00:03:01,770 what you use is up to you ultimately, up to your preference, 39 00:03:01,800 --> 00:03:04,260 you got exactly the same service in both ways. 40 00:03:04,470 --> 00:03:06,930 I just wanted to show the alternatives here.