如何从 Nodejs 更改 hello.sh 文件的文件权限
我已编写此代码,并希望从我的节点运行我的 hello.sh
文件。它失败了,错误是
错误:命令失败:./hello.sh >输出.txt /bin/sh: 1: 无法创建output.txt: 权限被拒绝
如何将 hello.sh
文件的权限
更改为可执行
。
fs.chmod("hello.sh",0o777,(err)=>{
if(err){
console.log(err)
return
}
})
exec("./hello.sh > output.txt", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
I have this code written and want to run my hello.sh
file from my node. it fails the error is
error: Command failed: ./hello.sh > output.txt
/bin/sh: 1: cannot create output.txt: Permission denied
How can I change the permission
of hello.sh
file to executable
.
fs.chmod("hello.sh",0o777,(err)=>{
if(err){
console.log(err)
return
}
})
exec("./hello.sh > output.txt", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样:
您还需要确保 hello.sh 运行的目录是可写的。
因为执行的命令是:
hello.sh > output.txt
由于错误消息是:
无法创建output.txt:权限被拒绝
它必须是不可写的。
Chmod 包含 hello.sh 的目录以使其可写,或将 exec 命令更改为:
./hello.sh > /tmp/output.txt
Try like this :
You also need to make sure that the directory where hello.sh is running is writable.
Because the executed command is:
hello.sh > output.txt
As the error message is:
cannot create output.txt: Permission denied
it must not be writable.
Chmod the directory that contain hello.sh to make it writable, or change your exec command to:
./hello.sh > /tmp/output.txt
创建输出文件时,首先在
/tmp
中创建它们以避免此类问题。然后将它们移动到需要去的地方,重新读取它们以异步传输它们,等等。When creating output files, first create them in
/tmp
to avoid these kind of issues. Then move them to where they need to go, re-read them to stream them async, etc.