5

Using these ps options I cut 10 lines and order output of ps by cpu usage (pcpu):

ps -eo pcpu,etime,pid,user,args --no-headers| sort -t. -nk1,2 -k4,4 -r |head -n 10

I need also show the name of columns (like same commands without cutting the output).

%CPU     ELAPSED   PID USER     COMMAND
 0.0 10-23:41:11     1 root     init [2]  
 0.0 10-23:41:11     2 root     [kthreadd]

2 Answers 2

10
ps -eo pcpu,etime,pid,user,args --sort=-%cpu | head -11
1
  • 3
    It should probably be head -11 if he wants the top 10 processes.
    – GregL
    Apr 1, 2015 at 17:40
1

This works on Centos 7. Two separate ps commands. First ps command to get the headers. Then a semicolon. Then the second ps command with your specific grep. The headers used are my standard ones.

ps -eo user,pid,ppid,lstart,etime,cmd |head -1;ps -eo user,pid,ppid,lstart,etime,cmd | grep <whatever you are grepping for |grep -v grep

Let's break it down with each part on a separate line.

ps -eo user,pid,ppid,lstart,etime,cmd |head -1
;
ps -eo user,pid,ppid,lstart,etime,cmd 
| grep <whatever you are grepping for>
| grep -v grep

Actual output:

[root@myserver ~]# ps -eo user,pid,ppid,lstart,etime,cmd |head -1;ps -eo user,pid,ppid,lstart,etime,cmd |grep sshd |grep -v grep

USER        PID   PPID                  STARTED     ELAPSED CMD
root       4673      1 Tue May 17 09:05:14 2022 122-09:29:11 /usr/sbin/sshd -D
root     233733   4673 Tue Sep 13 15:12:12 2022  3-03:22:13 sshd: kbanyas [priv]
kbanyas  234288 233733 Tue Sep 13 15:12:21 2022  3-03:22:04 sshd: kbanyas@pts/0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .