如何解决shelljs和nodejs中的子进程模块中的拒绝权限错误



我使用shelljs模块在节点中为服务器端程序执行bash文件。我甚至尝试了子进程模块来执行bash文件。在这两种情况下,我都得到了拒绝许可的错误。我使用的是linux(ubuntu-disto(。这是代码片段。

var shell = require("shelljs");
var cp = require("child_process");
shell.exec('../scripts/test.sh');
cp.exec('../scripts/test.sh', (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});

这是文件结构。

|-Parent file
|-routes
--file containing the above javascript code
|-scripts
--test.sh

这是错误。

/bin/sh: 1: ../scripts/test.sh: Permission denied
/bin/sh: 1: ../scripts/test.sh: Permission denied
exec error: Error: Command failed: ../scripts/test.sh
/bin/sh: 1: ../scripts/test.sh: Permission denied

有人能告诉我为什么会出现这个错误以及如何解决吗
注意-我在test.sh中没有使用任何sudo命令。只是一个简单的echo命令,比如echo"Hello World">

您需要运行chmod +x ../scripts/test.sh才能授予运行脚本文件的访问权限。如果失败,请尝试使用Sudo来运行节点脚本。

最新更新