可以使用EXEC运行终端命令

发布于 2025-01-24 15:57:52 字数 616 浏览 3 评论 0原文

我正在尝试在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 :

terminal

While this is the output if I directly write on terminal :

terminal

Any idea about how can I fix it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2025-01-31 15:57:52

您应该使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文