[英]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. 我有一个用jsp / servlets和tomcat 7编写的Web应用程序,它将在linux系统上运行。
I want to run a bash script with root privilages( gksudo ./myScript.sh
). 我想以root特权( gksudo ./myScript.sh
)运行bash脚本。 My script is under /web directory.( /web/myScript.sh
). 我的脚本在/ web目录下。(/web/ /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!! 我看到了一些例子,我设法运行了bash shell命令(如ls
),但没有设法执行脚本! How i can set as working directory the /web/
? 我如何将/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: 要执行脚本,可以使用如下的Runtime类 :
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. 我从未尝试过使用sudo(或gksudo)运行脚本,因此您将不得不使用它。
Hopefully you can just add it to the array before the script name. 希望您可以将其添加到脚本名称之前的数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.