【问题标题】:How to run sh script from node.js project?如何从 node.js 项目运行 sh 脚本?
【发布时间】:2020-05-01 02:03:02
【问题描述】:

我的 sh 脚本突然关闭。我正在尝试通过另一个脚本控制 sh 进程。 Nohup 对我没有帮助,所以我决定永远使用我的 Node.js。所以我找到了 child_process lib,但不知道如何在上面运行 sh 脚本。

【问题讨论】:

  • 你能添加你已经尝试过的代码吗?
  • 这能回答你的问题吗? Execute a command line binary with Node.js
  • @Titulum 我的代码与下面链接中的代码没有什么不同。我只是找不到链接到特定 .sh 文件的代码。所以我不知道该怎么做

标签: node.js sh child-process forever


【解决方案1】:

根据您对问题的评论,我认为您想要的是:

const { exec } = require('child_process')

exec('path/to/your/specific/file.sh', (err, stdout, stderr) => {
    // do whatever you want
});

路径可以是相对的或绝对的,文件必须是可执行的。

另一种方法是显式调用sh

const { exec } = require('child_process')

exec('sh path/to/your/specific/file.sh', (err, stdout, stderr) => {
    // do whatever you want
});

【讨论】:

  • 泰。我已经找到了一个解决方案(相同的代码但使用'shelljs' lib)。我只是使用没有 sh 前缀的exec('./file.sh'...。这对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 2012-11-24
  • 2014-05-04
  • 1970-01-01
相关资源
最近更新 更多