MongoDB Tips: Kill long running processes

This is just a quick and simple script to find all active opid that has been running over 10 seconds and they are running against the databases and collections you can specify in the "ns" filter.

It's best to first run

db.currentOp(
   {
     "active" : true,
     "secs_running" : { "$gt" : 10 },
     "ns" : {$in:["equipment.detachment","personnel.detail","personnel.unavailability","personnel.detachment"]}
   }
).inprog.forEach(function (doc) {
print(doc.opid);
})

and verify your output before you execute the kill command in my experience to make sure you are killing the processes that are meant to be killed.

Once verifed, simply run:

db.currentOp(
   {
     "active" : true,
     "secs_running" : { "$gt" : 10 },
     "ns" : {$in:["<db1>.<col1>","<db2>.<col2>","<dbn>.<coln>"]}
   }
).inprog.forEach(function (doc) {
db.killOp(doc.opid);
})

Comments

Popular posts from this blog

MongoDB tip: 4 ways to modify replica set configuration

MongoDB Quick Note: BI Connector Issue