我已经编写了这段代码,并希望从我的节点运行我的hello.sh
文件。它失败了,错误是
错误:命令失败:/你好.sh>output.txt/bin/sh:1:无法创建output.txt:权限被拒绝
如何将hello.sh
文件的permission
更改为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}`);
});
创建输出文件时,首先在/tmp
中创建它们以避免此类问题。然后将它们移动到需要的位置,重新读取它们以异步方式传输,等等。
这样尝试:
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}`);
});
});
您还需要确保运行hello.sh的目录是可写的。
因为执行的命令是:hello.sh > output.txt
由于错误消息为:cannot create output.txt: Permission denied
它不能写。
更改包含hello.sh的目录以使其可写,或者将exec命令更改为:./hello.sh > /tmp/output.txt