1

I want my directory to give executable permissions (by default) to all the shell scripts which are going to be created in that directory after I run my Java program. I tried the following commands:

setfacl -d -m group:name:rwx /path/to/your/dir

find ./ -name "*.sh" -exec chmod +x {} \;

The first one is not showing any response while the second one works fine but I have to execute this command manually in terminal after my Java program has created all the scripts. This is not what i seek. I want this thing to be automatic. Here is what I am trying to achieve:

My Java program creates the .sh files in a directory. Now the program would try to execute this script file.

Here is a Java code snippet which shows how it is going to execute the script files:

    ExecuteShellComand obj = new ExecuteShellComand();        
    String command2 = "./script.sh";
    String output2 = obj.executeCommand(command2);

It doesn't run unless I give the executable permissions to the script.sh. How do I do it? Is there any way around it? If I am not doing something in a way it should be done, feel free to give your suggestions. Thanks

  • Possible duplicate of [Running a shell script from java code](http://stackoverflow.com/questions/30622160/running-a-shell-script-from-java-code) – c1moore Dec 11 '16 at 20:55
  • I have found the answer from the link you shared. It really helped man. Thank you. – Usama Saeed Dec 12 '16 at 11:25

1 Answers1

1

Default ACL permissions are masked by the file's creation mode as specified in open and mkdir syscalls.

Since files are usually created with a default mode of 0666, execute permissions will be masked instead of inherited.

It's the responsibility of the program that creates the files to create them with the right permissions. If you set the permissions correctly when creating the scripts, you won't need ACL or chmod at all. The best way to fix this would be for your program to set the mode in the open call to 0777.

Java appears to have Files.createFile for this. Additionally, you have a more fuzzy File.setExecutable to do it after the fact, which is not Unix canonical behavior, but probably fine for your use case.

that other guy
  • 116,971
  • 11
  • 170
  • 194