简体   繁体   中英

Running Shell script on server

I have an Stand alone Application which runs Shell Script(with parameters)in Ubuntu.

ProcessBuilder pb1 = new ProcessBuilder("sh","deltapackage_app.sh","part_value","pathtofile"); 
Process process1 = pb1.start();

I am taking parameter through GUI. Now same thing i want to implement in web application where i can take inputs form web page and send it to server and then server will execute the shell script with parameters.

Can any one suggest me the best way of doing this. What things should i use to do this.

I Know i have to learn many things about server. Or can i use same code with Browser based Application.

Consider the following line of code:

 Process p = Runtime.getRuntime().exec("/bin/sh -c /bin/ls > ls.out"); 

This is intended to execute a Bourne shell and have the shell execute the ls command, redirecting the output of ls to the file ls.out. The reason for using /bin/sh is to get around the problem of having stdout redirected by the Java internals. Unfortunately, if you try this nothing will happen. When this command string is passed to the exec() method it will be broken into an array of Strings with the elements being "/bin/sh", "-c", "/bin/ls", ">", and "ls.out". This will fail, as sh expects only a single argument to the "-c" switch. To make this work try:

    String[] cmd = {"/bin/sh", "-c", "/bin/ls > out.dat"};
    Process p = Runtime.getRuntime().exec(cmd);

Since the command line is already a series of Strings, the strings will simply be loaded into the command array by the exec() method and passed to the new process as is. Thus the shell will see a "-c" and the command "/bin/ls > ls.out" and execute correctly.

http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html

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