Process Execution Shell Script

Source: Internet
Author: User

Overview:

The process class is an abstract class (all methods are abstract) and encapsulates a process (that is, an executing program).
The process class provides a way to perform input from the process, perform output to the process, wait for the process to complete, check the exit status of the process, and destroy (kill) the process.
The Processbuilder.start () and Runtime.exec methods create a native process and return an instance of the process subclass that can be used to control the process and obtain relevant information. The child process created does not have its own terminal or console. All of its standard IO (i.e., STDIN,STDOUT,STDERR) operations are redirected to the parent process through three streams (Getoutputstream (), getInputStream (), Geterrorstream ()) and read through the stream.

Api:

Destroy ()
Kill the child process.
Exitvalue ()
Returns the exit value of the child process.
InputStream Geterrorstream ()
Gets the error stream for the child process.
InputStream getInputStream ()
Gets the input stream of the child process.
OutputStream Getoutputstream ()
Gets the output stream of the child process.
WaitFor ()
Causes the current thread to wait, if necessary, until the process represented by that process object has been terminated

Attention:

1.destroy () The status of the child thread is killed in five minutes before the process disappears

2. Execute the linux command, if the command contains some special characters, you need to encapsulate processbuilder to get the process object

public class Command extends Thread {
Private Log logger = Logfactory.getlog (Command.class);

/**
* Determine if thread execution is complete
*/
public Boolean iscompletion;

/**
* Local Thread Object
*/
public process process;
/**
* Execution Results
*/
Public String rs= "";

Private Shellexecreq req;

Public Command (Shellexecreq req) {
This.req = req;
This.iscompletion = false;
}

/**
* @see java.lang.runnable#run ()
*/
@Override
public void Run () {
try {
Processbuilder p = New Processbuilder ("/bin/bash", "-C", Req.getcommand ());
Process = P.start ();
InputStream in = Process.getinputstream ();
Rs=ioutils.getstringfromreader (New BufferedReader (New InputStreamReader (in, "GBK"));
Rs=rs.replaceall ("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
Logger.info ("Execute script return Result:" +rs);
In.close ();
//program execution completed
Iscompletion = true;
InputStream input = Process.geterrorstream ();
if (input! = null) {
if (Stringutils.isblank (RS)) {
rs = ioutils.getstringfromreader (new BufferedReader (
New InputStreamReader (input));
}
Logger.error (Ioutils.getstringfromreader (New BufferedReader (new InputStreamReader (
Input)));
}
} catch (IOException e) {
rs = shellexecerrormessage.execerror;
Logger.error ("Execution Script exception:" + E.getmessage (), E) ;
}
}

/**
* Interrupt Running Thread
* @author Yang Xue October 31, 2014
*/
public void Kill () {
Terminate Server thread run
This.process.destroy ();
Break Thread in
This.interrupt ();
}
}

Process Execution Shell Script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.