Home > OS >  How to change file permission of hello.sh file from Nodejs
How to change file permission of hello.sh file from Nodejs

Time:03-15

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}`);
});

CodePudding user response:

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.

CodePudding user response:

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}`);
  });
});
  • Related