可以使用EXEC运行终端命令
我正在尝试在nodejs中使用EXEC函数,但它不起作用。
这是我正在运行的代码:
import { exec } from "child_process";
exec("ssh ubuntu@myserverip", (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
else {
console.log(`stdout:\n${stdout}`);
}
});
它返回的代码:
,
如果我直接输出在终端上写入:
我对如何修复它有任何想法吗?
i'm trying to use exec function in nodejs, but it's not working.
Here is the code that i'm runing :
import { exec } from "child_process";
exec("ssh ubuntu@myserverip", (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
else {
console.log(`stdout:\n${stdout}`);
}
});
And that it what it returns :
While this is the output if I directly write on terminal :
Any idea about how can I fix it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用Spawn,Exec用于执行其结果将立即交付给我的命令,或者在很短的时间内它必须快速,输出或将向我传递的结果的位置是不是很大,
因为EXEC会创建一个缓冲区(内存空间),它将开始存储命令的所有输出,并且除此之外,它具有超时,如果命令在那个时候没有完成,则它会杀死该过程。
You should use spawn, exec is used to execute commands whose result is going to be delivered to me immediately, or in a very short time, it has to be fast and where the output or the result that is going to be delivered to me is not very large,
because exec creates a Buffer (a memory space) where it will start storing all the output of the command, and besides that it has a timeout, and if the command does not finish in that time it kills the process.