如何解决 shelljs 和 Node js 子进程模块中的权限被拒绝错误?
我正在使用 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"
I am using shelljs module to execute a bash file in node for a server side program. I even tried child-process module to execute the bash file. In both cases I got permission denied error. I am using linux(ubuntu disto). Here is the code snippet.
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}`);
}
});
Here is the file structure.
|-Parent file
|-routes
--file containing the above javascript code
|-scripts
--test.sh
Here is the error.
/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
Can anyone suggest me why this error is occurring and how to resolve it?
Note- I have not used any sudo commands in the test.sh . Just a simple echo command like echo "Hello World"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要运行 chmod +x ../scripts/test.sh 来授予运行脚本文件的访问权限。如果失败,请尝试使用 Sudo 运行节点脚本。
You need to run
chmod +x ../scripts/test.sh
to give access to run the script file. If that fails, try to use Sudo to run the node script.