简体   繁体   English

节点子进程的cwd无法访问子文件夹

[英]cwd of node child process can't access subfolder

I have problems declaring the path for the current working directory of my child process. 我在声明子进程当前工作目录的路径时遇到问题。 I want to execute some R-Scripts and I want to save the results in a folder. 我想执行一些R脚本,并将结果保存在一个文件夹中。 But it does not work. 但这行不通。

This is working code in my server.js 这是我的server.js中的工作代码

app.post('/execScript', function (req, res) {
  var childProcess = require('child_process');
  var project = req.body.project;
  var script = req.body.script;
  childProcess.exec('Rscript ../app/projects/'+project+'/Scripts/'+script+'',{cwd: '../app/'}, (err) => {
    if (err) {
      console.error(err);
    }
  })
});

Note, that the cwd saves all results in the /app folder folder. 请注意,cwd将所有结果保存在/ app文件夹文件夹中。

When I try the following, it stops working and gives me an error. 当我尝试以下操作时,它停止工作并给我一个错误。

app.post('/execScript', function (req, res) {
  var childProcess = require('child_process');
  var project = req.body.project;
  var script = req.body.script;
  childProcess.exec('Rscript ../app/projects/'+project+'/Scripts/'+script+'',{cwd: '../app/test/'}, (err) => {
    if (err) {
      console.error(err);
    }
  })
});

I basically want to choose a subfolder of /app and it doesn't work. 我基本上想选择/ app的子文件夹,但是它不起作用。 I get the following error: 我收到以下错误:

{ Error: Command failed: Rscript ../app/projects/das_ist_ein_Test/Scripts/test.R
at ChildProcess.exithandler (child_process.js:211:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:885:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:501:12)   killed: false,   

code: 2,   
signal: null,  
 cmd: 'Rscript ../app/projects/das_ist_ein_Test/Scripts/test.R' }

Why is that? 这是为什么? Any help would be appreciated ! 任何帮助,将不胜感激 !

EDIT: I made sure that the subfolder exists and that i wrote it correctly. 编辑:我确保子文件夹存在并且我正确编写了它。

I got it. 我知道了。 The error occurs because of the way child processes work in node. 由于子进程在节点中的工作方式而发生错误。 When given a cwd, you start in that directory and after that you have to move to the file that needs to be executed from you cwd. 当给定一个cwd时,您将从该目录开始,然后,您必须移至需要从cwd执行的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM