linux 在应用程序中 使用shell命令获取数据

目录

1、top

2、借助函数popen

3,使用system()


在程序中使用命令行执行的结果

比如使用top命令查看系统状态

1、top

root@ubuntu:~# top

top - 05:26:18 up 6 days, 22:52,  1 user,  load average: 0.14, 0.14, 0.37
Tasks: 429 total,   1 running, 327 sleeping,   2 stopped,   0 zombie
%Cpu(s):  0.6 us,  1.0 sy,  0.0 ni, 98.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8117312 total,   772760 free,  2561432 used,  4783120 buff/cache
KiB Swap:  2097148 total,  2096624 free,      524 used.  5193880 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                          
  1732 wy        20   0 4381796 336160  84612 S   7.3  4.1  21:07.54 gnome-shell                                                                      
  1604 wy        20   0  579688 119156  74032 S   3.7  1.5  11:06.95 Xorg                                                                             
  2067 wy        20   0  851416  60504  36636 S   2.3  0.7   4:46.19 gnome-terminal-                                                                  
  1754 wy        20   0 2382772  17284  13476 S   0.7  0.2  94:00.18 pulseaudio                                                                       
  1771 wy        20   0  428516   8548   6368 S   0.7  0.1   1:49.78 ibus-daemon                                                                      
     1 root      20   0  225804   9436   6564 S   0.3  0.1   0:30.64 systemd                                                                          
   477 root     -51   0       0      0      0 S   0.3  0.0   0:28.41 irq/16-vmwgfx                                                                    
  1886 wy        20   0  652236  25248  16596 S   0.3  0.3   3:58.37 gsd-color                                                                        
  2407 wy        20   0 4633804 597844 254212 S   0.3  7.4  26:07.75 firefox         

想截取系统的CPU使用率,如何在C程序中使用这个值呢?

root@ubuntu:~# top -b -n1  | grep -n %Cpu | cut -d ':' -f 3| cut -d ',' -f 2
  0.2 sy

2、借助函数popen

popen()函数通过创建一个管道,调用fork()产生一个子进程,执行一个shell以运行命令来开启一个进程。注意函数关闭是pclose()

可以直接提取结果,不需要中转

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void)
{
    char*   cmd       = "top -b -n1  | grep -n %Cpu | cut -d ':' -f 3| cut -d ',' -f 2";
    FILE*   fp  = NULL;
    char    buf[1024] = {0};

    fp = popen(cmd, "r");
    if (NULL == fp)
    {
        printf("popen faild. (%d, %s)\n",errno, strerror(errno));
        return -1;
    }

    fread(buf, 1, sizeof(buf), fp);

    printf("res: %s\n", buf);

    pclose(fp);

    return 0;
}
root@ubuntu:~# ./a.out 
res:   0.2 sy

3,使用system()

结果可以写在文件中,再从文件中获取想要的数据。

程序先将结果保存在a.txt中,可以再从a.txt中获取数据。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void)
{
    system("top -b -n1  | grep -n %Cpu | cut -d ':' -f 3| cut -d ',' -f 2 > a.txt");
    return 0;
}
~       
root@ubuntu:~# cat a.txt 
  0.2 sy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为了维护世界和平_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值