I want to restart mongodb(the code is in a .bat file) by nodejs , if it was stopped by accident.
Is there any module can do this job?
It would be much better to install MongoDB as a Windows Service instead of running from a .bat file.
Then you can use the normal service features such as automatic startup and recovery.
Based on your latest two comments, there's a few things that you should take note of.
You are unable to restart the mongo windows service because there is a lock file. The CPU increases because mongod is attempting an automatic restart.
When the mongod process/service is uncleanly shutdown, there will be a lock file under your data path. Perform a dir
on the data directory and I believe that you will find the lock file mongod.lock
there.
You do not need to reboot your PC, simply remove the lock file (you may need to disable the service to do that) and restart the service.
There two SERVER tickets that are related to the restart behaviour on Windows -
Ticket 3582 - this fix (where an automatic restart of mongod
no longer occurs) is from version 2.1.0 onwards. 2.1.x is the development branch.
MongoDB 2.2.0-rc0 was recently released and is ready for testing. It is the culmination of the 2.1.x development series.
2.2 Release Notes: http://docs.mongodb.org/manual/release-notes/2.2
Downloads: http://www.mongodb.org/downloads
Change Log: https://jira.mongodb.org/browse/SERVER/fixforversion/11218
Ticket 2, which is currently in the planning stage and will be a longer-term fix.