原 java调用shell脚本
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1500
一:代码
public static void main(String[] args) {
log.info("开始调用shell脚本");
try {
Runtime runtime = Runtime.getRuntime(); // 创建 Runtime 对象
Process process = runtime.exec("sh /data/postgresql-11/pgbak/xiaobo.sh"); // 执行 shell 命令或脚本
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); // 获取命令的输出流
String line;
while ((line = reader.readLine()) != null) {
log.info("syncData {}", line);
}
int exitCode = process.waitFor(); // 等待命令执行完毕并获取返回值
log.info("exit code: {}", exitCode);
} catch (Exception e) {
e.printStackTrace();
}
log.info("结束调用shell脚本");
}
参考资料:
https://blog.csdn.net/qq_35861084/article/details/143684318
2025-06-03 09:28:33 阅读(9)
名师出品,必属精品 https://www.91mszl.com