我使用了top -b -n 2 | awk '{if ($9 != 0.0) print $0,sed -n '8,20p'}'>>text.txt
,那么输出是:
top - 16:24:42 up 3:49, 3 users, load average: 0.23, 0.29, 0.31
Threads: 503 total, 2 running, 501 sleeping, 0 stopped, 0 zombie
%Cpu(s): 9.7 us, 1.6 sy, 0.0 ni, 88.5 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 3938668 total, 2148708 used, 1789960 free, 133524 buffers
KiB Swap: 3903484 total, 0 used, 3903484 free. 822904 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1054 root 20 0 258664 3524 2692 S 0.5 0.1 0:00.00 rsyslogd
1086 root 20 0 258664 3524 2692 S 0.4 0.1 0:00.03 in:imuxsock
1087 root 20 0 258664 3524 2692 S 0.3 0.1 0:00.00 in:imklog
1090 root 20 0 258664 3524 2692 S 0.3 0.1 0:00.05 rs:main Q:Reg
2452 fpiat 20 0 25292 7520 3580 S 0.3 0.2 0:00.69 bash
2495 fpiat 20 0 25088 6988 3256 S 0.3 0.2 0:00.05 bash
top - 16:24:42 up 3:49, 3 users, load average: 0.23, 0.29, 0.31
Threads: 503 total, 2 running, 501 sleeping, 0 stopped, 0 zombie
%Cpu(s): 9.7 us, 1.6 sy, 0.0 ni, 88.5 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 3938668 total, 2148708 used, 1789960 free, 133524 buffers
KiB Swap: 3903484 total, 0 used, 3903484 free. 822904 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1054 root 20 0 258664 3524 2692 S 0.5 0.1 0:00.00 rsyslogd
1086 root 20 0 258664 3524 2692 S 0.4 0.1 0:00.03 in:imuxsock
1087 root 20 0 258664 3524 2692 S 0.3 0.1 0:00.00 in:imklog
1090 root 20 0 258664 3524 2692 S 0.3 0.1 0:00.05 rs:main Q:Reg
2452 fpiat 20 0 25292 7520 3580 S 0.3 0.2 0:00.69 bash
2495 fpiat 20 0 25088 6988 3256 S 0.3 0.2 0:00.05 bash
我希望得到如下所示的输出(这意味着未打印标题):
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1054 root 20 0 258664 3524 2692 S 0.5 0.1 0:00.00 rsyslogd
1086 root 20 0 258664 3524 2692 S 0.4 0.1 0:00.03 in:imuxsock
1087 root 20 0 258664 3524 2692 S 0.3 0.1 0:00.00 in:imklog
1090 root 20 0 258664 3524 2692 S 0.3 0.1 0:00.05 rs:main Q:Reg
2452 fpiat 20 0 25292 7520 3580 S 0.3 0.2 0:00.69 bash
2495 fpiat 20 0 25088 6988 3256 S 0.3 0.2 0:00.05 bash
1054 root 20 0 258664 3524 2692 S 0.5 0.1 0:00.00 rsyslogd
1086 root 20 0 258664 3524 2692 S 0.4 0.1 0:00.03 in:imuxsock
1087 root 20 0 258664 3524 2692 S 0.3 0.1 0:00.00 in:imklog
1090 root 20 0 258664 3524 2692 S 0.3 0.1 0:00.05 rs:main Q:Reg
2452 fpiat 20 0 25292 7520 3580 S 0.3 0.2 0:00.69 bash
2495 fpiat 20 0 25088 6988 3256 S 0.3 0.2 0:00.05 bash
有人能提出解决方案吗?
答案1
命令:top -b -n1| sed -n '/PID/,$p'
输出
ux_example ~]# top -b -n1| sed -n '/PID/,$p'
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7 root RT 0 0 0 0 S 1.7 0.0 0:01.04 migration/1
1 root 20 0 19356 1564 1252 S 0.0 0.1 0:07.96 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.07 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:03.46 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:01.84 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
6 root RT 0 0 0 0 S 0.0 0.0 1:58.23 watchdog/0
8 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/1
9 root 20 0 0 0 0 S 0.0 0.0 0:02.87 ksoftirqd/1
10 root RT 0 0 0 0 S 0.0 0.0 2:10.65 watchdog/1
11 root RT 0 0 0 0 S 0.0 0.0 0:02.42 migration/2
12 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/2
13 root 20 0 0 0 0 S 0.0 0.0 0:01.27 ksoftirqd/2
14 root RT 0 0 0 0 S 0.0 0.0 1:55.75 watchdog/2
15 root RT 0 0 0 0 S 0.0 0.0 0:01.03 migration/3
16 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/3
17 root 20 0 0 0 0 S 0.0 0.0 0:00.95 ksoftirqd/3
18 root RT 0 0 0 0 S 0.0 0.0 1:58.07 watchdog/3
19 root 20 0 0 0 0 S 0.0 0.0 0:14.40 events/0
20 root 20 0 0 0 0 S 0.0 0.0 3:52.96 events/1
21 root 20 0 0 0 0 S 0.0 0.0 0:16.77 events/2
22 root 20 0 0 0 0 S 0.0 0.0 1:34.48 events/3
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cgroup
24 root 20 0 0 0 0 S 0.0 0.0 0:00.01 khelper
25 root 20 0 0 0 0 S 0.0 0.0 0:00.00 netns
26 root 20 0 0 0 0 S 0.0 0.0 0:00.00 async/mgr
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 pm
28 root 20 0 0 0 0 S 0.0 0.0 0:07.44 sync_supers
29 root 20 0 0 0 0 S 0.0 0.0 0:03.43 bdi-default
30 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/0
31 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/1
32 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/2
33 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/3
34 root 20 0 0 0 0 S 0.0 0.0 0:02.02 kblockd/0
35 root 20 0 0 0 0 S 0.0 0.0 0:01.02 kblockd/1
36 root 20 0 0 0 0 S 0.0 0.0 0:07.83 kblockd/2
37 root 20 0 0 0 0 S 0.0 0.0 0:22.51 kblockd/3
38 root 20 0 0 0 0 S 0.0 0.0 0:
答案2
该top
命令用于交互式使用。要以编程方式获得所需的输出,您可以考虑使用它ps
,它可以提供大部分相同的信息,但对脚本更友好。
例子:
$ ps -eo pid,uid,pri,nice,vsz,rss,s,%cpu,%mem,time,comm | head
PID UID PRI NI VSZ RSS S %CPU %MEM TIME COMMAND
1 0 19 0 186380 9608 S 0.0 0.0 00:01:15 systemd
2 0 19 0 0 0 S 0.0 0.0 00:00:00 kthreadd
3 0 39 -20 0 0 I 0.0 0.0 00:00:00 rcu_gp
4 0 39 -20 0 0 I 0.0 0.0 00:00:00 rcu_par_gp
6 0 39 -20 0 0 I 0.0 0.0 00:00:00 kworker/0:0H-kblockd
8 0 39 -20 0 0 I 0.0 0.0 00:00:00 mm_percpu_wq
9 0 19 0 0 0 S 0.0 0.0 00:00:19 ksoftirqd/0
10 0 41 - 0 0 I 0.0 0.0 00:02:20 rcu_preempt
11 0 41 - 0 0 S 0.0 0.0 00:00:24 rcuc/0
请注意唯一缺少的字段是SHR
。 (我不知道如何ps
打印它。)
答案3
摘要区域的各个组件可以启用或禁用,如下所示:
Summary-Area-defaults
'l' - Load Avg/Uptime On (thus program name)
't' - Task/Cpu states On (1+1 lines, see '1')
'm' - Mem/Swap usage On (2 lines worth)
'1' - Single Cpu On (thus 1 line if smp)
要禁用所有这些,请开始,top
然后按每个m、l和t键,直到这些行消失(其中一些在几种模式之间循环,而不是简单的开关切换)。然后将更改保存到.toprc
with W。
之后,top -b -n 1
将仅显示空白行、列标题行以及每个进程的单独行。
配置文件在很大程度上是人类可读的,因此您可以直接编辑它,但它似乎不是为此设计的。 (值得注意的是,该fieldscur
字段包含通常 ASCII 范围之外的字符。)您要禁用的部分均由条目中的位表示winflags
,但具体值(以及配置文件的位置)可能因不同版本而异。