I have a Node.JS server, with a MongoDB database. I start the database within Node.JS as follows:
(function startDatabase() {
exec('killall -15 mongod; mongod --dbpath ./db', function (err, stdout, stderr) {
if(err) {
console.error('Database error! Aborting.');
process.exit(1);
}
});
}());
The problem with this approach is that I have no way of knowing that the database has successfully started, because mongod never returns (unless there is an error), so I never get to see stdout.
How can I start the MongoDB database within Node.JS and detect when it has started?
You can do :
service mongod status //check status
service mongod start //start
service mongod stop //stop
service mongod restart //restart
or instead of service mongod /etc/init.d/mongod
startDatabase should use service mongod start not kill the service and then start. It is not graceful close and you can loose data/corrupt files. If you want to restart it then use service mongod restart. But check status to stop or restart.