2013-02-18 56 views
0

我有一个Node.JS服务器,带有一个MongoDB数据库。我开始Node.js的内部数据库,如下所示:数据库启动时检测

(function startDatabase() { 
    exec('killall -15 mongod; mongod --dbpath ./db', function (err, stdout, stderr) { 
     if(err) { 
      console.error('Database error! Aborting.'); 
      process.exit(1); 
     } 
    }); 
}()); 

这种方法的问题是,我不知道该数据库已成功启动的方式,因为mongod不会返回(除非有错误)所以我永远不会看到stdout

如何在Node.JS中启动MongoDB数据库并检测它何时启动?

+0

你可以执行'exec('ps aux | grep mongod')'并检测它的输出,你也可以得到pid并使用'ps'来查看该进程是否正在运行。 – Sammaye 2013-02-18 10:17:06

+0

很奇怪你的系统没有提供管理服务的方法。 你的系统是什么? – Floby 2013-02-18 10:25:10

+0

您的意思是操作系统?它是Red Hat EL 6. – Randomblue 2013-02-18 10:26:17

回答

-1

你可以这样做:或

service mongod status //check status 
service mongod start  //start 
service mongod stop  //stop 
service mongod restart //restart 

代替service mongod/etc/init.d/mongod

startDatabase应该使用service mongod start不能杀死服务,然后启动。这不是优雅的关闭,你可以释放数据/损坏的文件。如果你想重新启动它,然后使用service mongod restart。但检查状态停止或重新启动。