简体   繁体   中英

How do you set user work as root in Linux, when the bash script runs via Java code

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?

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. ie

sudo permisions.sh

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM