java shell 异步执行_Java调用 shell脚本阻塞

Java在调用Shell时,要不断读取进程中标准输出和错误输出流的信息,否则缓冲区被写满就会造成子进程阻塞而无法继续运行下去,可起两个线程不断读取标准输出、错误流信息而不被阻塞import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;

import ch.ethz.ssh2.ChannelCondition;

import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.Session;

public class RmtShellExecutor {

private Connection conn;

private String ip;

private String usr;

private String psword;

private String charset = Charset.defaultCharset().toString();

private static final int TIME_OUT = 1000 * 5 * 60;

public RmtShellExecutor(String ip, String usr, String ps) {

this.ip = ip;

this.usr = usr;

this.psword = ps;

}

private boolean login() throws IOException {

conn = new Connection(ip);

conn.connect();

return conn.authenticateWithPassword(usr, psword);

}

//本地执行方法

public static void execLocal(String cmd) {

try {

Process proc = Runtime.getRuntime().exec(cmd);

StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");

StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");

errorGobbler.start();

outputGobbler.start();

int exitVal = proc.waitFor();

System.out.println("ExitValue: " + exitVal);

} catch (Exception e) {

e.printStackTrace();

}

}

//远程执行方法

public int execRemote(String cmds){

InputStream stdOut = null;

InputStream stdErr = null;

int ret = -1;

try {

if (login()) {

// Open a new {@link Session} on this connection

Session session = conn.openSession();

// Execute a command on the remote machine.

session.execCommand(cmds);

GobblerThread gtOut = new GobblerThread(session.getStdout(),"STD_OUT");

GobblerThread gtErr = new GobblerThread(session.getStderr(),"STD_ERR");

gtOut.start();

gtErr.start();

session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);

ret = session.getExitStatus();

} else {

}

}catch(Exception e){

e.printStackTrace();

}

finally {

if (conn != null) {

conn.close();

}

IOUtils.closeQuietly(stdOut);

IOUtils.closeQuietly(stdErr);

}

return ret;

}

public static void main(String args[]) throws Exception {

RmtShellExecutor exe = new RmtShellExecutor("192.168.3.5", "test",

"123456");

System.out.println(exe.execRemote("sh /home/test/cmd.sh company=32"));

}

}

class GobblerThread extends Thread

{

InputStream is;

String type;

GobblerThread(InputStream is, String type)

{

this.is = is;

this.type = type;

}

public void run()

{

try

{

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line=null;

while ( (line = br.readLine()) != null)

System.out.println(line);

} catch (IOException e)

{

e.printStackTrace();

}

}

}

execLocal 为本地调用cmd的方法

exeRemote 可利用ssh登录远程机器调用cmd的方法

参考文章:http://www.linuxidc.com/Linux/2014-04/99557.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值