1

I need to be able to run '/etc/init.d/mongod status' or 'service mongod status' from wihtin a node js file, in order to store the response in the database.

When I run the above commands in the command line, I get the following response:

● mongod.service - SYSV: Mongo is a scalable, document-oriented database.
   Loaded: loaded (/etc/rc.d/init.d/mongod)
   Active: active (running) since Thu 2017-02-02 08:07:42 UTC; 3h 27min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 793 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS)
 Main PID: 1027 (mongod)
   CGroup: /system.slice/mongod.service
           └─1027 /usr/bin/mongod -f /etc/mongod.conf

However, I want to include this status in an API response that I write. Therefore, when a user request my API, I want it to return the mongoDB status check as seen above.

I have tried the following ways:

router.get('/status', function(req, res) {

var databaseCheck = service mongod status // not sure how to do this
res.json({
    mongoResponse: '//to have the above status check response here'
});

});

I am new to all this, so any help would be appreciated. I may understand that my thinking is wrong - do let me know if there is a different way of doing this please

2
  • send a dummy request with ajax and if it fails you have the status? Commented Feb 2, 2017 at 11:53
  • Is there no way to do it from one file? I havent done ajax before Commented Feb 2, 2017 at 11:56

3 Answers 3

2

Connect a database and then check connection like db.serverConfig.isConnected(). The below code is a full example.

const app = express();

let dbClient;
let db;
let collection;

MongoClient.connect(configuration.mongoDbUri, { useNewUrlParser: true, poolSize: 30 }, (error, client) => {
  if (error) {
    console.log("Connection failed for some reason. Err: ", error);
    return error;
  }
  db = client.db("myDB");
  dbClient = client;
  collection = db.collection('myCollection');
  app.locals.mongoDb = db;
});

app.get("/status", (req, res) => {
  if (db.serverConfig.isConnected()) {   
    console.log("db.serverConfig.isConnected :", db.serverConfig.isConnected());
    return res.send({ result: true});
  }
  return res.send({ result: false});
});

app.listen(configuration.app.port, error => {});

Sign up to request clarification or add additional context in comments.

Comments

1

You can use nodejs child-process module to run a shell command like you would from a terminal. In a terminal you would "service mongod status", in the nodejs child-process you would do the same by putting that command as an argument to the child-process execute function, like so:

const exec = require('child_process').exec;
exec('service mongod status', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

2 Comments

I get the following : SyntaxError: Unexpected token > on this part ' stderr) => {'
you might have a nodejs version that doesn't support the new function arrow syntax, change the arrows with a normal javascript function exec('service mongod status', function(error, stdout, stderr){...})); If it gives you error about the ${stdout} and the rest of the variables with the $, just change that notation with the normal string concatenation like console.log('stderr: ' + stderr); Make sure you use the right characters while writing this.. If you notice you can't just copypaste the function I wrote because it's not using ' but it's using `, which are different characters...
0

Try code like this into you app:

db.serverConfig.isConnected();

2 Comments

Where db variable come from?
From method when you created the instance of the client and connected to MongoDB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.