简体   繁体   中英

How to run a bash script in java-ee web application?

I have a web application writen in jsp/servlets & tomcat 7.It will run on linux systems.

I want to run a bash script with root privilages( gksudo ./myScript.sh ). My script is under /web directory.( /web/myScript.sh ). How can i do that??

I have seen some examples, i managed to run bash shell command(like ls ), but didn't managed to execute the script!! How i can set as working directory the /web/ ?

Check out the following method to figure out how to get the path to your script:

To execute a script, you can use the Runtime class like this:

String[] commandArray = {
    "/bin/bash",
    "-c",
    "my_script.sh"
}

Runtime.getRuntime().exec(commandArray);

I've never tried running a script using sudo (or gksudo), so you'll have to play around with that.

Hopefully you can just add it to the array before the script name.

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