简体   繁体   English

Node.js-产生的进程正在生成错误“ execvp():没有这样的文件或目录”

[英]Node.js - spawned process is generating error “execvp(): No such file or directory”

I have the following code that's intended to spawn and detach a child process, which is just another node.js script in the same directory. 我有以下旨在产生和分离子进程的代码,这只是同一目录中的另一个node.js脚本。 Here's the exact code I'm running: 这是我正在运行的确切代码:

var fs = require('fs');
var child = require('child_process');

var out = fs.openSync('/tmp/daemon.log', 'a');
var options = {
    cwd: process.cwd(),
    env: process.env,
    detached: true,
    stdio: ['ignore', out, process.stderr]
};

child.spawn('/usr/local/bin/node ./daemon.js', [], options).unref();

All daemon.js does right now is exit after a timeout of two seconds: daemon.js现在所做的所有daemon.js都是在两秒钟的超时后退出:

setTimeout(function() {
    console.log('done');
}, 2000);

If I run daemon.js directly from the terminal it works as expected. 如果我直接从终端运行daemon.js ,它将按预期工作。 If I run the same command being passed to child.spawn() it works as expected. 如果我运行传递给child.spawn()的同一命令,它将按预期工作。 However when I run the script it generates this error: 但是,当我运行脚本时,它将生成此错误:

execvp(): No such file or directory

It doesn't seem node.js specific and I'm having trouble working out what the problem is. 似乎不是特定于node.js的,并且我在弄清楚问题出在哪里时遇到了麻烦。 Does anybody have any suggestions? 有人有什么建议吗?

For reference, this is on OS X 10.8.5 Server with node installed using Homebrew at path /usr/local/Cellar/node/0.10.25/bin/node and symlinked to /usr/local/bin/node . 作为参考,这是在OS X 10.8.5服务器上,其节点使用Homebrew在路径/usr/local/Cellar/node/0.10.25/bin/node安装,并符号链接到/usr/local/bin/node

EDIT 编辑

The code now works perfectly, with the added avdantage of it not mattering which working-directory the main script is run from! 现在,代码可以完美运行,并且添加了avdantage,与运行主脚本的工作目录无关!

var fs = require('fs');
var child = require('child_process');

var out = fs.openSync('/tmp/daemon.log', 'a');
var options = {
    cwd: process.cwd(),
    env: process.env,
    detached: true,
    stdio: ['ignore', out, process.stderr]
};

child.spawn('/usr/local/bin/node', [__dirname + '/daemon.js'], options).unref();

Try changing 尝试改变

child.spawn('/usr/local/bin/node ./daemon.js', [], options).unref();

To: 至:

child.spawn('/usr/local/bin/node', ['./daemon.js'], options).unref();

Or: 要么:

child.spawn('node', ['daemon.js'], options).unref();

试一试

__dirname+'/daemon.js'

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

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