Shell scripts are powerful and simple when working with text and managing the operating system.
Combining shell scripts into your application is a great way to quickly implement
This article describes the use of Java code to invoke and execute the shell
I wrote jbosslogdelivery.sh in the ~/bin/directory, with two features.
The {./jbosslogdelivery.sh list} function is to output the list of existing JBoss logs,
{./jbosslogdelivery.sh get LogName [email protected]} is the role of sending the specified log to the specified mailbox
I intend to integrate this script in a Web application to operate the script via HTTP GET.
Here is the core code
It is recommended to use Processbuilder to create a processing process, the parameter cmds is a string array,
One way to get popular online is runtime.getprocess, I'm testing my own directory when I execute a script.
According to the above script, the output log list function Cmds The value {"jbosslogdelivery.sh", "list"}
The CMDS value of the Extract log function is {"jbosslogdelivery.sh", "Get", "logname", "[Email protected]"}
Processbuilder processbd=new Processbuilder (CMDS);
Switch to the directory where the script is located
Processbd.directory (New File (ScriptPath)); ScriptPath = "~/bin/";
Start execution
Process = Processbd.start ();
Execution results
int status = Process.waitfor ();
Read the output of the script
br = new BufferedReader (New InputStreamReader (Process.getinputstream ()));
Processing results
String line = "";
while (line =br.readline ())!=null) {
Reslist.add (line);
}
Java calls shell commands and scripts