问题描述
我有以下代码旨在产生和分离子进程,这只是同一目录中的另一个node.js脚本.这是我正在运行的确切代码:
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. 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();
所有daemon.js
现在所做的都是在两秒钟的超时后退出:
All daemon.js
does right now is exit after a timeout of two seconds:
setTimeout(function() {
console.log('done');
}, 2000);
如果我直接从终端运行daemon.js
,它将按预期工作.如果我运行传递给child.spawn()
的相同命令,则它将按预期工作.但是,当我运行脚本时,它将生成此错误:
If I run daemon.js
directly from the terminal it works as expected. If I run the same command being passed to child.spawn()
it works as expected. However when I run the script it generates this error:
execvp(): No such file or directory
似乎不是特定于node.js的,我在弄清楚问题出在哪里时遇到了麻烦.有人有什么建议吗?
It doesn't seem node.js specific and I'm having trouble working out what the problem is. Does anybody have any suggestions?
作为参考,这是在OS X 10.8.5服务器上,该服务器使用Homebrew在路径/usr/local/Cellar/node/0.10.25/bin/node
上安装了节点,并符号链接到/usr/local/bin/node
.
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
.
编辑
代码现在可以完美运行了,添加的avdantage与运行主脚本的工作目录无关!
The code now works perfectly, with the added avdantage of it not mattering which working-directory the main script is run from!
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();
推荐答案
尝试更改
child.spawn('/usr/local/bin/node ./daemon.js', [], options).unref();
收件人:
child.spawn('/usr/local/bin/node', ['./daemon.js'], options).unref();
或者:
child.spawn('node', ['daemon.js'], options).unref();
这篇关于Node.js-产生的进程正在生成错误"execvp():没有这样的文件或目录";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!