import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* 记录当前内存信息
*
* @author Sx
* @version 2013-10-08
*/
public class TopFileUtils {
private static final Logger logger = LoggerFactory.getLogger(TopFileUtils.class); public static String writeTopMsg() {
InputStreamReader isr = null;
BufferedReader br = null; FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null; String result = "";
try {
String cmd = "top -b -n 1";// 直接写top输出为空,动态命令,只能加参数输出一次
Process ps = Runtime.getRuntime().exec(cmd);
isr = new InputStreamReader(ps.getInputStream());
br = new BufferedReader(isr);
File file = new File("/opt/topmsg.txt");
if (!file.exists()) {
file.createNewFile();
} fos = new FileOutputStream(file, true);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw); StringBuffer sb = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
bw.write(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
logger.error("writeTopMsg error:" + e);
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
logger.error("close bw error:" + e);
}
try {
if (osw != null)
osw.close();
} catch (IOException e) {
logger.error("close osw error:" + e);
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
logger.error("close fos error:" + e);
}
try {
if (br != null)
br.close();
} catch (IOException e) {
logger.error("close br error:" + e);
}
try {
if (isr != null)
isr.close();
} catch (IOException e) {
logger.error("close isr error:" + e);
}
}
return result;
} public static void main(String[] args) {
System.out.println(writeTopMsg());
}
}

java记录linux top命令信息的更多相关文章

  1. linux top命令查看内存及多核CPU的使用讲述

    查看多核CPU命令 mpstat -P ALL  和  sar -P ALL 说明:sar -P ALL > aaa.txt   重定向输出内容到文件 aaa.txt top命令 经常用来监控l ...

  2. Linux top命令的用法详细详解

    首先介绍top中一些字段的含义: VIRT:virtual memory usage 虚拟内存1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等2.假如进程申请100m的内存,但实际只使用 ...

  3. 【转载】linux top命令查看内存及多核CPU的使用讲述

    转载 https://www.cnblogs.com/dragonsuc/p/5512797.html 查看多核CPU命令 mpstat -P ALL  和  sar -P ALL 说明:sar -P ...

  4. (转)Linux top命令的用法详细详解

    原文:https://yq.aliyun.com/articles/399004?spm=a2c4e.11153940.blogcont399002.9.3a19f00aHOA3SH# 摘要: 首先介 ...

  5. linux top命令查看内存及多核CPU的使用讲述【转】

    转载一下top使用后详细的参数,之前做的笔记找不见了,转载一下,作为以后的使用参考: 原文地址:http://blog.csdn.net/linghao00/article/details/80592 ...

  6. (转)详解Linux Top 命令

    top 命令是最流行的性能监视工具之一,我们必需了解.它是一个优秀的交互式工具,用于监视性能.它提供系统整体性能,但报告进程信息才是 top 命令的长处.top 命令交互界面如下图所视:

  7. 新手指南:详解Linux Top 命令

    Linux top命令简介 top 命令是最流行的性能监视工具之一,我们必需了解.它是一个优秀的交互式工具,用于监视性能.它提供系统整体性能,但报告进程信息才是 top 命令的长处.top 命令交互界 ...

  8. (转)linux TOP命令各参数详解【转载】

    实时监控或查看系统资源使用情况的工具——TOP top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. 下面详细介绍它的使用方法: ( ...

  9. linux TOP命令各参数详解【转载】

    实时监控或查看系统资源使用情况的工具——TOP top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. 下面详细介绍它的使用方法: ( ...

随机推荐

  1. 【BZOJ 3242】 (环套树、线段树+树形DP?)

    3242: [Noi2013]快餐店 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 728  Solved: 390 Description 小T打算 ...

  2. WordPress插件会员简化1.58 -任意文件下载漏洞(附poc)

    今天我们将讨论在WordPress插件WordPress插件与重点会员简化v1.58作为这个剧本的创作时间不打补丁的扶贫开发实践.脆弱脚本如下: CVE-ID:cve-2017-1002008 当然, ...

  3. [bzoj1022][SHOI2008]小约翰的游戏 John (博弈论)

    Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取 ...

  4. bzoj 1143: [CTSC2008]祭祀river / 2718: [Violet 4]毕业旅行 -- 二分图匹配

    1143: [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MB Description 在遥远的东方,有一个神秘的民族,自称Y族.他们 ...

  5. DP练习 巡逻

     国庆这天五一大道上人头攒动,这是因为大家都准备从五一广场上那个大屏幕观看新中国60周年的国庆阅兵式!这虽然是一件很喜庆的事情,可却让CS市的警察局长伤透了脑筋的,因为人潮拥挤是很容易发生安全事故的. ...

  6. HDU 5640 King's Cake GCD

    King's Cake 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5640 Description It is the king's birthd ...

  7. Unity 的一些特性

    using System; using UnityEngine; using UnityEditor; using UnityEngine.Serialization; using Random = ...

  8. JSP myecplise项目移植到ecplise

    把myecplise项目移植到ecplise的一些细节: 参考于http://www.cnblogs.com/liushuijinger/p/3396063.html 因为个人需要,需要把JSP项目从 ...

  9. Transistor latch improves on/off circuitry

    Figure 1 shows an example of on/off circuitry commonly used in battery-operated devices. The p-chann ...

  10. FPGA LVDS I/O as an Analog Programmable Comparator

    http://www.eetimes.com/author.asp?section_id=36&doc_id=1320289 Seeing the new ADC IP being bandi ...