如何从 node.js 项目 运行 sh 脚本?

How to run sh script from node.js project?

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

根据您在问题下的评论,我假设您想要的是:

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
});