【问题标题】:Python: need to use bash one-linerPython:需要使用 bash one-liner
【发布时间】:2013-06-08 18:44:58
【问题描述】:

我正在尝试编写一个脚本来帮助我的公司进行一些调查。我无法在系统上安装任何额外的软件包,我需要能够获得当前处理器负载的百分比。

到目前为止,我找不到任何允许这样做的本机 python 模块,所以我正在尝试使用 bash 命令: ps aux|awk 'NR > 0{s +=$3};END{print "CPU %",s}'

我一直在 os.system 和 os.popen 中尝试过,但似乎无法正常执行。

import os, multiprocessing
p = os.system("ps aux|awk 'NR > 0 { s +=$3 }; END {print "cpu %",s}'")
print "Cores:", multiprocessing.cpu_count(), '\nCPU Load:', p

我可以使用一两个提示。我知道您应该在 os.system 中用“,”分隔命令和参数,但我已经尝试过,但似乎无法让它工作。有谁知道在 Python 中执行长单行的更好方法?

我是 python 新手,感谢您给出的任何答案。谢谢。

【问题讨论】:

    标签: python linux cpu


    【解决方案1】:

    试试

    import os, multiprocessing
    p = os.popen("ps aux|awk 'NR > 0 { s +=$3 }; END {print s}'").read().rstrip("\n")
    print "Cores:", multiprocessing.cpu_count(), '\nCPU Load:', p, "%"
    

    给予

    Cores: 4
    CPU Load: 5.1 %
    

    【讨论】:

    • 做到了。谢谢你。不过,两者都是很好的答案。
    【解决方案2】:

    你就快到了:只需要“转义”引号(即使用 print \" 而不是 print " 等)来正确显示第一行:

    import os, multiprocessing
    p = os.system("ps aux|awk 'NR > 0 { s +=$3 }; END {print \"cpu %\",s}'")
    

    并且您应该认识到os.system 命令返回“返回码”——“此过程是否正常结束?”问题的答案。因此p 的值是0“无错误”,而不是变量s 中的cpu 负载。不知道为什么你需要它两次,因为你已经打印出来了。将最后一行改为

    print "Cores:", multiprocessing.cpu_count(), '\n'
    

    输出变成这样:

    cpu% 65.7
    Cores: 2 
    

    如果您真的想将系统命令的输出直接放入变量中,您可以查看this earlier question 的答案——它向您展示了如何使用subprocess.Popen()proc.communicate() 来做到这一点。但是,正确使用命令行(使用转义的引号)是最关键的解决方法。

    更新以下是 Popen() 和 system() 方法的两个完整且有效的示例:

    import os, multiprocessing, subprocess
    # two ways to get CPU load and number of CPUs
    
    # command line string to obtain CPU load:
    commandString = "ps aux|awk 'NR > 0 { s+=$3 }; END {print \"CPU%\", s}'"
    print "Command string used to obtain CPU load:"
    print commandString, "\n"
    
    # obtain number of CPUs from the multiprocessing function:
    numCPUs = multiprocessing.cpu_count()
    
    # method 1: issue an os.system command, have the result echoed directly to output:
    print "Method 1:"
    p = os.system(commandString)
    print "Cores:", numCPUs, "\n"
    
    # method 2: return the value to a python variable:
    cpu = subprocess.Popen(commandString, stdout=subprocess.PIPE, shell=True)
    (out, err) = cpu.communicate()
    print "Method 2:"
    print "Load: ", out,
    print "Cores:", multiprocessing.cpu_count(), '\n'
    

    以上输出:

    Command string used to obtain CPU load:
    ps aux|awk 'NR > 0 { s+=$3 }; END {print "CPU%", s}' 
    
    Method 1:
    CPU% 18.5
    Cores: 2 
    
    Method 2:
    Load:  CPU% 24.1
    Cores: 2 
    

    【讨论】:

    • 感谢您的回答。但是,仍然无法正常工作。我查看了该帖子并修改了我的脚本:import subprocess, multiprocessing cpu = subprocess.Popen(["ps aux|awk 'NR > 0{s +=$3;END{print \"CPU %\",s'"], stdout=subprocess.PIPE, shell=True) (out, err) = cpu.communicate() print "Cores:" multiprocessing.cpu.count(), '\n' print "Load", out 我仍然遇到错误。它在第二个 's' 处返回“意外的换行符或字符串结尾”。感谢您的转义报价。没有考虑到这一点。 :)
    • 您的命令中有几个拼写错误:您正在打开 { 而没有关闭它们......我已经更新了我的答案“为了完整性” - 请注意,将命令字符串放在它自己的变量中您可以更清楚地看到这两个调用之间的区别。
    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 2014-03-20
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多