Skip to content

Instantly share code, notes, and snippets.

@hapiben
Forked from comerford/killLongRunningOps.js
Created December 15, 2015 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hapiben/3d80502012ff1cf27193 to your computer and use it in GitHub Desktop.
Save hapiben/3d80502012ff1cf27193 to your computer and use it in GitHub Desktop.
// kills long running ops in MongoDB (taking seconds as an arg to define "long")
// attempts to be a bit safer than killing all by excluding replication related operations
// and only targeting queries as opposed to commands etc.
killLongRunningOps = function(maxSecsRunning) {
currOp = db.currentOp();
for (oper in currOp.inprog) {
op = currOp.inprog[oper-0];
if (op.secs_running > maxSecsRunning && op.op == "query" && !op.ns.startsWith("local")) {
print("Killing opId: " + op.opid
+ " running for over secs: "
+ op.secs_running);
db.killOp(op.opid);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment