java执行shell脚本类_java执行shell脚本

http://blog.csdn.net/arkblue/article/details/7897396

一、直接调用版

使用到Process和Runtime两个类,返回值通过Process类的getInputStream()方法获取

package ark;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class ReadCmdLine {

public static void main(String args[]) {

Process process = null;

List processList = new ArrayList();

try {

process = Runtime.getRuntime().exec("ps -aux");

BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line = "";

while ((line = input.readLine()) != null) {

processList.add(line);

}

input.close();

} catch (IOException e) {

e.printStackTrace();

}

for (String line : processList) {

System.out.println(line);

}

}

}

调用shell脚本,判断是否正常执行,如果正常结束,Process的waitFor()方法返回0

publicstaticvoidcallShell(String shellString) {

try{

Process process = Runtime.getRuntime().exec(shellString);

intexitValue = process.waitFor();

if(0!= exitValue) {

log.error("call shell failed. error code is :"+ exitValue);

}

} catch(Throwable e) {

log.error("call shell failed. "+ e);

}

} 二、异步版

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Date;

import java.util.Properties;

public class ShellExec {

public static void execShell(String command) {

InputStreamReader stdISR = null;

InputStreamReader errISR = null;

Process process = null;

//String command = "/home/Lance/workspace/someTest/testbash.sh";

long timeout = 10 * 1000;

try {

process = Runtime.getRuntime().exec(command);

//CommandStreamGobbler errorGobbler = new CommandStreamGobbler(

//process.getErrorStream(), command, "ERR");

//CommandStreamGobbler outputGobbler = new CommandStreamGobbler(

//process.getInputStream(), command, "STD");

//errorGobbler.start();

// 必须先等待错误输出ready再建立标准输出

//while (!errorGobbler.isReady()) {

//Thread.sleep(10);

//}

//outputGobbler.start();

//while (!outputGobbler.isReady()) {

//Thread.sleep(10);

//}

CommandWaitForThread commandThread = new CommandWaitForThread(

process);

commandThread.start();

long commandTime = new Date().getTime();

long nowTime = new Date().getTime();

boolean timeoutFlag = false;

while (!commandThread.isFinish()) {

if (nowTime - commandTime > timeout) {

timeoutFlag = true;

break;

} else {

Thread.sleep(1000);

nowTime = new Date().getTime();

}

}

if (timeoutFlag) {

// 命令超时

//errorGobbler.setTimeout(1);

//outputGobbler.setTimeout(1);

System.out.println("正式执行命令:" + command + "超时");

}

//while (true) {

//if (errorGobbler.isReadFinish() && outputGobbler.isReadFinish()) {

//break;

//}

//Thread.sleep(10);

//}

} catch (IOException | InterruptedException e) {

e.printStackTrace();

} finally {

if (process != null) {

process.destroy();

}

}

}

public class CommandWaitForThread extends Thread {

private Process process;

private boolean finish = false;

private int exitValue = -1;

public CommandWaitForThread(Process process) {

this.process = process;

}

public void run() {

try {

this.exitValue = process.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

finish = true;

}

}

public boolean isFinish() {

return finish;

}

public void setFinish(boolean finish) {

this.finish = finish;

}

public int getExitValue() {

return exitValue;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package generconfig; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import org.springframework.context.support.GenericXmlApplicationContext; import com.pactera.service.schedule.ThreadReadPath; import com.pactera.util.CommTool; /*打包用File->Export->runnable JAR File Export Launch选择对应的 Export destination 选择地址 Library handling:Copy required */ public class SMSThreadSender { public static void main(String[] args) { if(args.length==0) { System.out.println("短信服务开启,开始加载Spring配置。"); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.setValidating(false); context.load("classpath:sysconfig/applicationContext.xml"); context.refresh(); System.out.println("开整·~~~~"); CommTool.smsthreadisruning=true; ThreadReadPath thread = new ThreadReadPath(); thread.smsname="sms"+CommTool.threadid; Thread t1 = new Thread(thread); t1.setName("sms"); t1.start(); System.out.println("已经启动"); } if(args.length==1) { String pfile=args[0]; System.out.println("短信服务开启,开始加载Spring配置。"+pfile); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.setValidating(false); context.load(pfile); // context.load("classpath:sysconfig/applicationContext.xml"); context.refresh(); CommTool.smsth

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值