java 调用Linux shell

来源:互联网 发布:景观设计大数据 编辑:程序博客网 时间:2024/06/01 18:57

java通过ssh操作linux服务器
http://blog.csdn.net/u012621115/article/details/53434548

调用命令

final Process process = Runtime.getRuntime().exec(command);BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

http://www.cnblogs.com/langtianya/p/6815976.html

特别需要注意的是,当需要执行的linux命令带有管道符时(例如:ps -ef|grep java),用上面的方法是不行的,解决方式是将需要执行的命令作为参数传给shell

final String[] command={"/bin/sh","-c","/usr/bin/cat /proc/cpuinfo | grep 'cpu cores' | uniq"};

http://blog.csdn.net/a19881029/article/details/8063758

command 命令可以是缩写,也可以加该命令的路径

/usr/bin/cat 是可以的cat 也是可以的

同时,| 无需转义

原创粉丝点击