0

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)
Amit Raj
  • 1,358
  • 3
  • 22
  • 47
  • Your second example doesn't make sense (`cp` is not defined while you're not doing anything with `result`). For the first example, [read the documentation](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) closely on how to use `spawn` – robertklep Aug 21 '17 at 14:23
  • Why not just use [fs.rename](https://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback)? – James Aug 21 '17 at 14:24
  • @james they want to make sure that the file isn't being used by another process. That said, at least on Unix-like OS'es, renaming a file that's still in use by another processes isn't an issue per se. – robertklep Aug 21 '17 at 14:29
  • @robertklep yeah what I was angling at, although in hindsight perhaps they don't want to be renaming files users have open more from of a UX point of view rather than a technical one. – James Aug 21 '17 at 14:38
  • @robertklep, I am trying to print the output of the command now. Once I will be able to print the output of the command, I proceed further. – Amit Raj Aug 21 '17 at 14:41

0 Answers0