Paul Stoner Paul Stoner - 1 month ago 10x
Node.js Question

Programmatically stopping MongoDB

I am working on an application using Node.JS, Electron. This application will run its own instance of MongoDB. The start up of Mongo is working using the following code:

child = childProcess.exec(`mongod --dbpath ${appConfig.dbConfigPath}`);

However, when the user exits the program, I want to stop mongo. I have tried the following, all taken from MongoDB Documentation

child = childProcess.exec('mongod --shutdown');


child = childProcess.exec(`kill -2 ${}`);

yet neither of these are shutting down the process.

This application is being developed to run on the windows platform.

For clarity, here is my app configuration file. The init() function is executed from within my main.js. The shutdown() is executed in the windowMain.on('close').


'use strict';

const childProcess = require('child_process');

const fileUtils = require('./lib/utils/fileUtils');
const appConfig = require('./config/appConfig');

let child;

class Calibration {
constructor() {}

init() {

shutdown() {

function createAppConfigDir() {

function createAppDataDir() {

function startMongo() {
child = childProcess.exec(`mongod --dbpath ${appConfig.dbConfigPath}`);

function shutdownMongo() {
console.log('inside shutdownMongo');
//This is where I want to shutdown Mongo

module.exports = new Calibration();


'use strict'

const { app, BrowserWindow, crashReporter, ipcMain: ipc } = require('electron');
const path = require('path');

const appCalibration = require('../calibration');


const appConfig = require('../config/appConfig');

let mainWindow = null;

ipc.on('set-title', (event, title) => {
mainWindow.setTitle(title ||;

ipc.on('quit', () => {

// Quit when all windows are closed.
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {

// Create the browser window.
mainWindow = new BrowserWindow({ center: true });


mainWindow.setMinimumSize(770, 400);

mainWindow.loadURL(path.join(`file://${__dirname}`, '../ui/index.html'));

mainWindow.on('close', () => {
console.log('Inside quit')

mainWindow.on('closed', function() {
mainWindow = null;

Any assistance is greatly appreciated.


You can use Ipc to send orders through your js files.

on your main.js where you defined your electron. you can put this:

ipcMain.on("shutDowmDatabase", function (event, content) {
    // shutdown operations.

Then in some part of your application code, you can put a function like this:

function sendShutdowmOrder (content){
   var ipcRenderer = require("electron").ipcRenderer;
   // the content can be a parameter or whatever you want that should be required for the operation.
   ipcRenderer.send("shutDowmDatabase", content);

Also i think you can use the events of Electron to shutdown your db, this listen the events of your mainWindow created when you start electron

    mainWindow.on('closed', function () {
        // here you command to shutdowm your data base.
        mainWindow = null;

For more information about Ipc you can see here and information about the events of your window here