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. 「UOJ218」火车管理

    「UOJ218」火车管理 解题思路:观察发现,在弹出 \(x\) 之前,它前面这个元素都是保持不变的,所以可以用一棵可持久化线段树维护每一个栈顶元素的插入时间,每次找到当前时间\(-1\) 的版本就可 ...

  2. SpringBoot 如何从前台传递数组

    1.SpringBoot 如何从前台传递数组 2.前台 $.ajax({ url: 'deleteBsGiftById', type: 'post', dataType: 'json', data: ...

  3. 【BZOJ】2724: [Violet 6]蒲公英

    2724: [Violet 6]蒲公英 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 2900  Solved: 1031[Submit][Statu ...

  4. 将多层级xml解析为Map

    /** * 解析xml的工具类 * 1.将多层级xml解析为Map */ public class ParseXmlUtil { public static final String TAG = &q ...

  5. iOS开发之int,NSInteger,NSUInteger,NSNumber的使用

    1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...

  6. 1036: [ZJOI2008]树的统计Count (树链剖分)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3401  Solved: 1418[Submit] ...

  7. 关于GCC Cygwin MinGW MSYS

    [转载]关于Gcc/MinGW/Cygwin/Msys http://blog.sciencenet.cn/blog-778757-616920.html 一.GCC的历史 GCC是一个原本用于Uni ...

  8. SQL Server 2000 ——DBCC命令

    http://blog.163.com/ruifeng_00/blog/static/6904584200971291923462/   一.定义 微软当初从SYBASE将DBCC是作为数据库一致性检 ...

  9. python笔记21-列表生成式

    前言 python里面[]表示一个列表,快速生成一个列表可以用range()函数来生成. 对列表里面的数据进行运算和操作,生成新的列表最高效快速的办法,那就是列表生成式了. range() 1.一个连 ...

  10. 第十一章 PhpMyAdmin连接远程mysql服务器---连接openwrt 703N服务器

    //千万不要在你原来的那个phpmyadmin文件夹上操作~~~要复制一个新的进行操作,这样我们就可以同时使用本地和远程   一.下载phpmyadmin到本地 我使用的是windows下的集成WAM ...