我正在使用,node.js v0.6.10尽管我在0.6.7. 基本上我运行一个子进程使用spawn启动另一个node.js的过程,并通过连通stdout和stdin 这里有两个脚本:
父 ( cli.js) :
var spawn = require("child_process").spawn;
var doSpawn = function(callback){
var child = spawn('child.js');
child.on('exit', function(code){
console.log("Child exited with code " + code);
});
child.stdin.write("ping");
child.stdin.end();
};
doSpawn();
setTimeout(function(){}, 10000);
Run Code Online (Sandbox Code Playgroud)
child.js
var run = function(){
process.stdout.on('drain', function(){
process.exit(0);
});
process.stdout.write(stdout);
};
var stdin = process.stdin;
stdin.resume();
stdin.setEncoding("utf8");
var stdout = '';
stdin.on('data', function(data){
stdout += data;
});
stdin.on('end', run);
Run Code Online (Sandbox Code Playgroud)
然后当我运行时node cli.js:
$ …Run Code Online (Sandbox Code Playgroud) 我无法在以下代码中获得任何输出:
var spawn = require('child_process').spawn,
script = 'ftp',
child = spawn(script);
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
child.on('close', function (code) {
console.log('child process exited with code ' + code);
});
Run Code Online (Sandbox Code Playgroud)
它适用于普通脚本,如“ls”、“pwd”等。但不适用于交互式程序,如“ftp”、“telnet”。有什么建议么?
编辑:
以另一个脚本为例:
#!/usr/bin/env python
name = raw_input("your name>")
print name
Run Code Online (Sandbox Code Playgroud)
当生成此脚本时,我希望使用数据事件获取提示“your name>”,以便稍后可以将某些内容输入到标准输入中。
问题是我在数据事件中什么也没有得到,而且似乎这些事件都没有被触发。
对于此处发布的代码长度,我深表歉意。我正在尝试创建一个类,该类使用 boost::process 来生成一个进程,在其 stdin 上为其提供一些数据并捕获其所有 stdout 和 stderr。
子进程的标准输入可能和标准输出一样冗长;目标机器没有大量内存,因此每个都需要分块处理。
我已经阅读了无数使用 boost::process 的例子,但没有发现任何可以一起回答所有这些问题的例子。我曾尝试将这些例子结合起来,但没有成功。我显然错过了一些东西。我将不胜感激任何帮助。
发生的情况是子进程成功生成但没有任何反应。父进程在标记为 THUS *的行上停止。
类定义:
class CommandProcessor {
public:
explicit CommandProcessor(const std::string &executable_path, bool slow) :
executable_path_(executable_path), slow_(slow), in_(io_service_, ::dup(STDIN_FILENO)), out_(io_service_, ::dup(STDOUT_FILENO)), err_(io_service_, ::dup(STDERR_FILENO)) {
}
private:
void begin_write_stdin();
void end_write_stdin(const boost::system::error_code &ec, std::size_t bytes_transferred);
void begin_read_stdout();
void end_read_stdout(const boost::system::error_code &ec, std::size_t bytes_transferred);
void begin_read_stderr();
void end_read_stderr(const boost::system::error_code &ec, std::size_t bytes_transferred);
public:
void execute_command(const Command& command);
private:
boost::filesystem::path executable_path_;bool slow_;
boost::asio::io_service io_service_;
boost::asio::posix::stream_descriptor in_;
boost::asio::posix::stream_descriptor out_;
boost::asio::posix::stream_descriptor err_; …Run Code Online (Sandbox Code Playgroud) 我的 nodejs 应用程序在接收特定请求时使用 spawn 生成一个子进程(我不能使用 exec,因为输出很大并且不知道我是否可以添加回调到生成的进程)来准备响应。
我希望子进程发送响应或主进程等待子进程准备响应并退出。
问题是主进程不等待子进程。
我已经编码了这样的东西
inputQuery: function(req, res){
var output="";
var query = "printjson(db.getCollectionNames())";
var temp = spawn("mongo", ["mongoweb ", "-eval", query]);
temp.on('error', function (err){
console.log(err);
});
temp.stdout.on('data', function(data){
output += data;
});
temp.stderr.on('data', function(data){
console.log(data);
});
temp.on("exit", function(code){
console.log("Output is :" + output);
res.send(output); // Either send response here or after the log message below
});
console.log("I want this to wait or let child respond");
}
Run Code Online (Sandbox Code Playgroud)
我真的被困住了,不知道该怎么做。请帮忙。提前致谢。
我尝试sshpass使用 node.js spawn() 运行命令来自动化 ssh。但它会自动从命令行退出,没有任何错误。这是我使用的代码。
var tail = spawn("sshpass", ["-p", "test", "ssh", "-n", "test@xxx.x.xxx.xxx", "'cat testfile'"]);
tail.stdout.on("data", function(data) {
readData(data);
});
Run Code Online (Sandbox Code Playgroud) 我们先来看看这个现象
Nodejs代码:
const cp = require('child_process');
var ls = cp.spawn('ls', ['/']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process closed with code ${code}`);
});
while(true){}
Run Code Online (Sandbox Code Playgroud)
运行这个nodejs代码,没有显示任何内容,似乎没有触发任何事件。
然后在另一个shell中运行“ps -ef | grep ls | grpe -v grep”,结果是:
liyuanq+ 10995 10990 0 11:06 pts/3 00:00:00 [ls] <defunct>
Run Code Online (Sandbox Code Playgroud)
如果删除代码:
while(true){}
Run Code Online (Sandbox Code Playgroud)
节点进程退出,并触发on data事件。
问题是为什么节点在实际完成其工作时不关闭生成的进程,直到父节点进程退出。
我的环境:
操作系统:Debian 8.4 x86_64
节点:v6.1.0
我正在使用这段代码:
const {
spawn
} = require('child_process');
let info = spawn('npm', ["-v"]);
info.on('close', () => {
console.log('closed');
}
Run Code Online (Sandbox Code Playgroud)
但我有这个错误:
events.js:182
throw er; // Unhandled 'error' event
^
Error: spawn npm ENOENT
at exports._errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:189:19)
at onErrorNT (internal/child_process.js:366:16)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)
at Function.Module.runMain (module.js:607:11)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
Run Code Online (Sandbox Code Playgroud)
如果我使用:
let info = spawn('npm', ["-v"], {shell: true});
Run Code Online (Sandbox Code Playgroud)
有用!
但为什么我需要shell: true?我还需要查看该生成的标准输出,所以我也使用这个:
let info = spawn('npm', ["-v"], {shell: true, stdio: 'inherit'});
Run Code Online (Sandbox Code Playgroud)
这是正确的?
我正在开发一个通过 gui 控制 gulp 任务的电子应用程序。您单击一个任务,它就会运行。很简单的东西。在 macOS 上,当我运行 npm start 时,它运行得很好,但是当我用 electro-packager 打包它时,出现以下错误:
Uncaught Exception:
Error: spawn gulp ENOENT
at exports._errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Run Code Online (Sandbox Code Playgroud)
这是代码:
exports.runTask = (taskName, projPath) => {
const cp = spawn('gulp', [ taskName ], {cwd: projPath});
cp.stdout.setEncoding('utf8');
cp.stdout.on('data', data => {
console.log(data);
mainWindow.webContents.send('task-console-data', data);
});
cp.stderr.setEncoding('utf8');
cp.stderr.on('data', data => {
console.error(data);
displayNotification({text: `[error] ${data}`});
mainWindow.webContents.send('task-console-data', `[error] ${data}`);
});
cp.on('exit', code => {
if (code === 0) { …Run Code Online (Sandbox Code Playgroud) 更新前云功能运行良好。这是现在发生错误的代码:
return spawn('convert' ,[tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}`, tempLocalThumbFile], {capture: ['stdout', 'stderr']});
Run Code Online (Sandbox Code Playgroud)
以下是错误的详细信息:
generateThumbnail Error: {
Error: spawn convert ENOENT at _errnoException
(util.js:1022:11) at Process.ChildProcess._handle.onexit
(internal/child_process.js:190:19) at onErrorNT
(internal/child_process.js:372:16) at _combinedTickCallback
(internal/process/next_tick.js:138:11) at process._tickDomainCallback
(internal/process/next_tick.js:218:9)
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn convert',
path: 'convert',
spawnargs: [
'/tmp/images/E32NIXQKgVUxjUGDmPkr_aaaa',
'-thumbnail',
'400x220',
'/tmp/images/thumb_E32NIXQKgVUxjUGDmPkr_aaaa'
]
}
Run Code Online (Sandbox Code Playgroud)
是我做错了什么还是这是 firebase 云错误?
imagemagick spawn firebase imagemagick-convert google-cloud-functions
我正在尝试通过 child_process spawn 在带有 ipc 选项的节点中生成命令。
我所说的:
const {spawn} = require('child_process');
const cmd = spawn('npm', ['-v'], {
shell: true,
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
});
cmd.on('message', (msg) => console.log(msg));
Run Code Online (Sandbox Code Playgroud)
我得到什么:
child_process.js:122
p.open(fd);
Error: EBADF: bad file descriptor, uv_pipe_open
Child (child_process.js:122:5)
at setupChildProcessIpcChannel (internal/bootstrap/pre_execution.js:329:30)
at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:54:3)
at internal/main/run_main_module.js:7:1 {
errno: -4083,
code: 'EBADF',
syscall: 'uv_pipe_open'
}
Run Code Online (Sandbox Code Playgroud)
这种情况仅发生在特殊配置中:
another.js,npmnode有一个已关闭的问题,没有多大用处。
shell: true根据这篇文章,我需要跨平台兼容性。
这个问题似乎也相关,但读完后我并没有变得更聪明。
节点 v12.18.3
感谢帮助。