有时候我们需要在Linux服务器上执行一些shell命令和脚本,Java给我们提供了这个解决方案,核心类:Runtime
代码清单:
ShellUtil.java
@Slf4j
public class ShellUtil {
/**
* 运行shell脚本
*
* @param shell 需要运行的shell脚本的地址 绝对路径
*/
public static void execShell(String shell) {
List<String> strList = new ArrayList<>();
List<String> errList = new ArrayList<>();
try {
Process process = Runtime.getRuntime().exec(shell);
process.waitFor();
InputStreamReader ir = new InputStreamReader(process.getInputStream());
InputStreamReader err = new InputStreamReader(process.getErrorStream());
LineNumberReader input = new LineNumberReader(ir);
LineNumberReader errInput = new LineNumberReader(err);
String line;
while ((line = input.readLine()) != null) {
strList.add(line);
}
while ((line = errInput.readLine()) != null) {
errList.add(line);
}
process.destroy();
ir.close();
err.close();
input.close();
errInput.close();
} catch (Exception e) {
log.error("运行shell脚本失败:",e);
}
log.debug("执行 {} ", shell);
log.debug("执行结果:{} ", strList);
log.debug("脚本错误信息:{}", errList);
}
/**
* 运行shell脚本 后台运行 new String[]方式
*
* @param shell 需要运行的shell脚本文件字符串
*/
public static void execShellBin(String shell) {
try {
Runtime.getRuntime().exec(new String[]{
"/bin/sh", "-c", shell}, null, null);
} catch (Exception e) {
log.error("运行shell脚本失败:",e);
}
}
/**
* 运行shell并获得结果,注意:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流
*
* @param shStr 需要执行的shell命令
*/
public static List<String> runShell(String shStr) {
List<String> strList = new ArrayList<>();
List<String> errList = new ArrayList<>();
try {
Process process = Runtime.getRuntime().exec(new String[]{
"/bin/sh", "-c", shStr}, null, null);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
InputStreamReader err = new InputStreamReader(process.getErrorStream());
LineNumberReader input = new LineNumberReader(ir);
LineNumberReader errInput = new LineNumberReader(err);
String line;
while ((line = input.readLine()) != null) {
strList.add(line);
}
while ((line = errInput.readLine()) != null) {
errList.add(line);
}
process.destroy();
ir.close();
err.close();
input.close();
errInput.close();
} catch (Exception e) {
log.error("运行shell脚本失败:",e);
}
log.debug("执行 {} ", shStr);
log.debug("执行结果:{} ", strList);
log.debug("脚本错误信息:{}", errList);
return strList;
}
public static void main(String[] args) {
//授权
execShellBin("cd /Users/${userName}/project/; chmod +x ./test.sh");
execShell("/Users/${userName}/project/test.sh");
execShellBin("ls ; pwd");
List<String> strings = runShell("ls ; pwd");
System.out.println(strings);
}
}
注意执行的脚本需要授权,同时shell文件,必须是UNIX格式,ANSI编码格式,windows电脑可以先在Linux服务器上编写了,再下载下来