- 
      
- 
        Save no7dw/ccb33255e185878dde2b0d0fda0e8c9d to your computer and use it in GitHub Desktop. 
    Find and (safely) kill long running MongoDB ops
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | db.currentOp().inprog.forEach( | |
| function(op) { | |
| if(op.secs_running > 5) printjson(op); | |
| } | |
| ) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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); | |
| } | |
| } | |
| }; | |
| //example: killLongRunningOps(5) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment