如何从 nodejs 子进程(在 windows 和 linuxish 中)获取 cwd(当前工作目录)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15939380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:10:38  来源:igfitidea点击:

How to get the cwd (current working directory) from a nodejs child process (in both windows and linuxish)

node.jsprocess

提问by B T

I'm trying to run a script via nodejs that does:

我正在尝试通过 nodejs 运行一个脚本,该脚本执行以下操作:

cd ..
doSomethingThere[]

However, to do this, I need to executed multiple child processes and carry over the environment state between those processes. What i'd like to do is:

但是,要做到这一点,我需要执行多个子进程并在这些进程之间传递环境状态。我想做的是:

var exec = require('child_process').exec;
var child1 = exec('cd ..', function (error, stdout, stderr) {
  var child2 = exec('cd ..', child1.environment, function (error, stdout, stderr) {
  });
});

or at very least:

或者至少:

var exec = require('child_process').exec;
var child1 = exec('cd ..', function (error, stdout, stderr) {
  var child2 = exec('cd ..', {cwd: child1.process.cwd()}, function (error, stdout, stderr) {
  });
});

How can I do this?

我怎样才能做到这一点?

回答by Andrey Sidorov

to start child with parent dir as cwd:

以父目录为 cwd 启动子项:

var exec = require('child_process').exec;
var path = require('path')

var parentDir = path.resolve(process.cwd(), '..');
exec('doSomethingThere', {cwd: parentDir}, function (error, stdout, stderr) {
  // if you also want to change current process working directory:
  process.chdir(parentDir);
});

Update: if you want to retrieve child's cwd:

更新:如果你想检索孩子的 cwd:

var fs = require('fs');
var os = require('os');
var exec = require('child_process').exec;

function getCWD(pid, callback) {
  switch (os.type()) {
  case 'Linux':
    fs.readlink('/proc/' + pid + '/cwd', callback); break;
  case 'Darwin':
    exec('lsof -a -d cwd -p ' + pid + ' | tail -1 | awk \'{print }\'', callback);
    break;
  default:
    callback('unsupported OS');
  }
}

// start your child process
//    note that you can't do like this, as you launch shell process 
//    and shell's child don't change it's cwd:
// var child1 = exec('cd .. & sleep 1 && cd .. sleep 1');
var child1 = exec('some process that changes cwd using chdir syscall');

// watch it changing cwd:
var i = setInterval(getCWD.bind(null, child1.pid, console.log), 100);
child1.on('exit', clearInterval.bind(null, i));     

回答by surfbuds

If you want to get the current working directory without resorting to OS specific command line utilities, you can use the "battled-tested" shelljs library that abstract these things for you, while underneath using child processes.

如果您想在不求助于操作系统特定的命令行实用程序的情况下获取当前工作目录,您可以使用“battled-tested”shelljs 库为您抽象这些东西,同时使用子进程。

var sh = require("shelljs");
var cwd = sh.pwd();

There you have it, the variable cwd holds your current working directory whether you're on Linux, Windows, or freebsd.

有了它,变量 cwd 保存您当前的工作目录,无论您使用的是 Linux、Windows 还是 freebsd。

回答by Bret Copeland

Just a thought, if you know the child process's PID, and have pwdxinstalled (likely on linux), you could execute that command from node to get the child's cwd.

只是想一想,如果您知道子进程的 PID,并且安装了pwdx(可能在 linux 上),您可以从节点执行该命令以获取子进程的 cwd。

回答by sjt003

I think the best bet is manipulating the options.cwdbetween calls to exec. in execcallback this.pwdand this.cwdmight give you leverage for your implementations.

我认为最好的办法是操纵options.cwd调用之间exec。在exec回调中this.pwdthis.cwd可能会为您的实现提供杠杆作用。