I need to check a particular file is open or close in node.js. If a particular file is close then we will rename it. When I run the following command on the terminal(Mac), it is giving me correct results.
lsof +D /Users/amitraj/Documents/traces
But when I am running the above command in node.js, it is giving me error. Following is source code:
var child =
cp.spawn('lsof +D /Users/amitraj/Documents/traces', []);
child.stdout.on('data', function(chunk) {
chunk.toString().split('\n').forEach(function(line) {
console.log(line);
});
});
child.stdout.on('end', function(chunk) {
"use strict";
console.log("end");
});
child.on('error', function(e) {
"use strict";
console.log(e);
});
I am getting the following error:
{ Error: spawn lsof +D /Users/amitraj/Documents/traces/ ENOENT
at exports._errnoException (util.js:1018:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:367:16)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
at Module.runMain (module.js:606:11)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn lsof +D /Users/amitraj/Documents/traces/',
path: 'lsof +D /Users/amitraj/Documents/traces/',
spawnargs: [] }
end
I also tried the following code but still got error:
let cmd = "lsof +D /Users/amitraj/Documents/traces";
var result = require('child_process').execSync(cmd);
cp.exec(cmd, (error, stdout, stderr) => {
//do whatever here
if (error) {
console.log(`error: `);
console.log(error);
}
if (stdout) {
console.log(`output : `);
console.log(stdout);
}
if (stderr) {
console.log(`stderr: `);
console.log(stderr);
}
});
Error:
Error: Command failed: lsof +D /Users/amitraj/Documents/traces
at checkExecSyncError (child_process.js:481:13)
at Object.execSync (child_process.js:521:13)
at Object.<anonymous> (/Users/amitraj/Downloads/notification/cs/cs.js:465:39)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)