1

Suppose my bash file contains

chmod -R 777 $backup_files/app/webroot/uploadedFiles/

It works fine when I run it through root permission using /permission.sh but when I try to run this bash script via java code like

        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("/permission.sh");
        printBufferedReaderOutputFromProcess(process);
        process.waitFor();

It gives me the following error:

chmod: changing permissions of `/home/app/webroot/uploadedFiles/A.jpg': Operation not permitted 

What can I fix it?

1

2 Answers 2

1

I suspect you haven't use sudo before, however an alternative to using the setuid on the script is to give the user the right to sudo the script as root. i.e.

sudo permisions.sh

This runs as root can can be run without asking a password. You have to set this up in sudoers.conf

Sign up to request clarification or add additional context in comments.

2 Comments

@PeterLawrey: Have you ever heard about setuid scripts?!? OMG! However sudo advise is OK (however sudo requires full path to the script if it is not in PATH e.g. sudo ./permisions.sh).
My first suggestion was to use a setuid script, however these can be hard to control who can run it and manage in any centralise way.
0

How are you running your Java code? Is it stand-alone, or running in a web container like Tomcat? If the later, it's probably running as a user that doesn't have permissions to the directory you're trying to change.

5 Comments

Yes, I am runnig java code through tomcat6 and user name is tomcat, I added the following line in sudoers tomcat ALL=(ALL) NOPASSWD: ALL
Are you calling sudo somewhere, maybe as part of permission.sh? (It doesn't just work automatically.)
Pritom: You should change your Jave code to runtime.exec("sudo /permission.sh");. Perhaps being more strict is good idea: tomcat ALL = NOPASSWD: /permission.sh.
When I write runtime.exec("sudo /permission.sh"); then it shows sudo: sorry, you must have a tty to run sudo. And when I write this in sudoers tomcat ALL = NOPASSWD: /permission.sh then it shows chmod: changing permissions of /app/webroot/uploadedFiles/': Operation not permitted
It may be possible to resovle the "sorry, you must have a tty to run sudo" issue by adding !requiretty to the directive in your sudoers file. However, similar to stackoverflow.com/questions/8410360/… , I really question the overall approach here. Maybe this is an initial setup that you need to complete outside of Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.