'How to change file permission of hello.sh file from Nodejs
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}`);
});
Solution 1:[1]
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.
Solution 2:[2]
Try like this :
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}`);
});
});
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | New Alexandria |
| Solution 2 |
