8

I'm running lsof in one of my Linux server to check if one file (/tmp/incoming_data.txt) in the ext3 system is opened by other programs. My server has lots of TCP connection on it. The weird thing is the execution of 'sudo lsof' takes about two minutes to finish and it uses 99.x% CPU during that two minutes.

The command I used is sudo lsof /tmp/incoming_data.txt. I've tried "fuser" which takes about the same amount CPU and execution time. Is there anything I can do fix this problem?

7ochem
  • 280
  • 1
  • 3
  • 12
James Gan
  • 376
  • 1
  • 5
  • 10
  • After those two minutes have passed, what does `lsof` show? How many processes and threads have that file open? What is the CPU load when not running lsof? – Michael Martinez Apr 08 '17 at 21:13

3 Answers3

18

Pass the -n option to skip resolving DNS names of IP connections. This will almost certainly be the biggestion portion of any slowdowns.

Zoredache
  • 128,755
  • 40
  • 271
  • 413
8

If you are certain that you can skip the open TCP or UDP connections, since you mentioned there are too many of them, you can use the dialect-specific option of lsof -X.

lsof -X

Read the man page of lsof and search for '-X' for detailed information.

Daniel t.
  • 9,061
  • 1
  • 32
  • 36
0

I've found this little NPM package which does a wonderful job of speeding up lsof for files: https://www.npmjs.com/package/lsof-mac-fast.

I created a wrapper script to use it:

node ~/tools/MacOs/lsof.js db.mv.db 1000
repeating using interval: 1000
COMMAND  PID  USER   FD   TYPE DEVICE  SIZE/OFF     NODE NAME
java    7336 jumar  256u   REG    1,4 194465792 53854404 
db.mv.db

This is my script:

// https://www.npmjs.com/package/lsof-mac-fast
var fastLsof = require('lsof-mac-fast');

var myArgs = process.argv.slice(2);
var fileToCheck = myArgs[0];
var repeatIntervalMs = myArgs[1];
// console.log('myArgs: ', myArgs);

function lsofFile(file) {
  fastLsof.lsof([file], function(err, stdout, stderr) {process.stdout.write(stdout)});
}

if (repeatIntervalMs) {
  // repeat until killed
  // https://javascript.info/settimeout-setinterval
  console.log('repeating using interval: ' + repeatIntervalMs);
  var timerId = setInterval(() => lsofFile(fileToCheck),  repeatIntervalMs);
} else {
  // just one time
  lsofFile(fileToCheck);
}

The repeat interval can be really low, e.g. 10 ms seems to work fine.

Juraj Martinka
  • 431
  • 3
  • 7