我有这个脚本,但我不知道如何获得打印输出中的最后一个元素:

cat /proc/cpuinfo | awk '/^processor/{print $3}'

最后一个元素应该是cpu数量减1。


当前回答

这很简单。只需使用这个命令:

lscpu

其他回答

cat /proc/cpuinfo | grep processor

这工作得很好。当我尝试第一个答案时,我得到了3个CPU的输出。我知道我在系统上有4个cpu,所以我只是为处理器做了一个grep,输出看起来像这样:

[root@theservername ~]# cat /proc/cpuinfo | grep processor
processor       : 0
processor       : 1
processor       : 2
processor       : 3

你也可以使用Python!获取物理核数:

$ python -c "import psutil; print(psutil.cpu_count(logical=False))"
4

获取超线程核的数量:

$ python -c "import psutil; print(psutil.cpu_count(logical=True))"
8

使用下面的查询获取核心细节

[oracle@orahost](TESTDB)$ grep -c ^processor /proc/cpuinfo
8
grep -c ^processor /proc/cpuinfo

将计算在/proc/cpuinfo中以“processor”开头的行数

对于具有超线程的系统,可以使用

grep ^cpu\\scores /proc/cpuinfo | uniq |  awk '{print $4}'

它应该返回(例如)8(而上面的命令将返回16)

lscpu以人类可读的格式收集CPU架构信息/proc/cpuinfon:

# lscpu


Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    1
Core(s) per socket:    4
CPU socket(s):         2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 15
Stepping:              7
CPU MHz:               1866.669
BogoMIPS:              3732.83
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0-7

参见https://unix.stackexchange.com/questions/468766/understanding-output-of-lscpu。