Process process = Runtime.getRuntime().exec("top -b -n 1");
//已测试
//运行shell 得到 linux cpu的使用率
public static String getCpuRateForLinux() {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader brStat = null;
StringTokenizer tokenStat = null;
try {
Process process = Runtime.getRuntime().exec("top -b -n 1");
is = process.getInputStream();
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);
osVersion = System.getProperty("os.version");
System.out.println("yang osVersion:"+osVersion);
if (osVersion.startsWith("2.6")){
System.out.println("yang osVersion.startsWith(2.6)");
String temp;
int end;
//取得cpu空闲 idle
Float usage =0.0f;
while((temp = brStat.readLine())!=null){
if(temp.indexOf("id")>-1){
end = temp.indexOf("id");
temp = temp.substring(end-9,end);
temp = temp.substring(temp.lastIndexOf(",")+1,temp.lastIndexOf("%"));
usage = Float.valueOf(temp.trim());
break;
}
}
//取得cpu空闲 idle
System.out.println("osVersion 2.6:"+(1.0f - usage/ 100));
//保留3位小数
return Math.round((1.0f - usage/ 100)*1000)/1000 +"";
}
}catch (IOException ioe) {
System.out.println(ioe.getMessage());
freeResource(is, isr, brStat);
return "1";
} finally {
freeResource(is, isr, brStat);
}
}
分享到:
相关推荐
需要提高cpu利用率的shell脚本
1、创建存放脚本文件目录 mkdir -p /etc/press chmod 777 /etc/press 2、启动定时任务进程 echo 'systemctl start crond.service...部署成功后查看当前使用率:top -n 1|grep Cpu|awk '{print "当前CPU使用率:"$2"%"}'
提高内存使用率的脚本
用shell编写的一个倒计时后查看cpu前十的进程
java调用shell 包含简单的编写shell文件 调用含有参数的shell文件
linux 下,监测进程CPU利用率,当某个进程达到设定的CPU利用率时报警(或其它动作)
详细的linux shell脚本启动java代码类。
java连接linux,执行shell命令的工具类 sshxcute.jar
linux 获取cpu shelllinux 获取cpu shelllinux 获取cpu shelllinux 获取cpu shell
linux系统java服务自启动shell脚本及服务cpu内存占用监控脚本
shell脚本,通过snmp方式获取远程服务器的CPU内存和硬盘的使用率,需要支持snmpget和snmpwalk命令,并且为了支持小数运算,需要安装bc命令。
java调用shell脚本java调用shell脚本java调用shell脚本java调用shell脚本java调用shell脚本java调用shell脚本
在java代码中调用执行shell脚本,sqlldr导数与使用sqlplus在shell调用执行存储过程。 linux环境中有2个dba的用户:oracle、erm 还有1个web用户:erm 在linux环境中,这三个用户都可以直接在任意目录下执行该shell...
通过linux的SSH端口使用java代码远程对指定的linux服务器上执行shell命令,实测有用
利用Runtime.execute方法,我们可以在Java程序中运行Linux的Shell脚本,或者执行其他程序
shell,bat脚本运行java程序, shell,bat脚本运行java程序,
linux shell脚本启动java。 ---------------------------------------- Windows编辑的sh,在linux会报错: shell unexpected end of file 解决办法: vim test.sh :set fileformat=unix :wq ----------------------...
sshxcute 就是这样一个框架工具集,它基于 JSCH 构建,允许工程师利用Java 代码通过 SSH 连接远程批量执行 Linux/UNIX 系统上的命令或者脚本,同时加入了判断成功与否,取回输出等多种实用功能。sshxcute 不管是针对...
java调用shell命令.pdf
java操作linux工具包