LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-24-2017, 04:30 PM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
sorting ps output


ps output sort on PID in ascending order:
Code:
$ ps -eo user,pid,pri,ni,cmd --sort=pid
USER       PID PRI  NI CMD
root         1  19   0 /sbin/init splash
root         2  19   0 [kthreadd]
root         3  19   0 [ksoftirqd/0]
root         5  39 -20 [kworker/0:0H]
root         7  19   0 [rcu_sched]
root         8  19   0 [rcu_bh]
root         9 139   - [migration/0]
... ...
ps output sort on PID in descending order:
Code:
$ ps -eo user,pid,pri,ni,cmd --sort=-pid
USER       PID PRI  NI CMD
foffb    10943  19   0 less
foffb    10942  19   0 ps -eo user,pid,pri,ni,cmd --sort=-pid
foffb    10900  19   0 /usr/lib/firefox/plugin-container -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appdir /usr/lib/firefox/browser 2719 true tab
root     10884  19   0 [kworker/u8:2]
root     10871  19   0 [kworker/3:2]
foffb    10800  19   0 bash
.... ....
But ps does not sort on ascending pri.
It also does not sort on decending order for pri.

Code:
$ ps -eo user,pid,pri,ni,cmd --sort=pri
USER       PID PRI  NI CMD
root         1  19   0 /sbin/init splash
root         2  19   0 [kthreadd]
root         3  19   0 [ksoftirqd/0]
root         5  39 -20 [kworker/0:0H]
root         7  19   0 [rcu_sched]
root         8  19   0 [rcu_bh]
root         9 139   - [migration/0]
root        10 139   - [watchdog/0]
root        11 139   - [watchdog/1]
root        12 139   - [migration/1]
root        13  19   0 [ksoftirqd/1]
root        15  39 -20 [kworker/1:0H]
root        16 139   - [watchdog/2]

.....
Why?
Thank you.
 
Old 01-24-2017, 06:00 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,821
Blog Entries: 15

Rep: Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664
You can pipe to the separate "sort" command to achieve what you want:

Sort on pri field (field 3) as a numeric field:
Code:
ps -eo user,pid,pri,ni,cmd |sort -nk3
Reverse the sort:
Code:
ps -eo user,pid,pri,ni,cmd |sort -rnk3
That sort command can be used for many things not just ps.


EDIT:
On doing tests it appears you have to spell out "priority" for the ps built in sort to work (i.e. don't just use "pri"):
Code:
ps -eo user,pid,pri,ni,cmd --sort=priority
Code:
ps -eo user,pid,pri,ni,cmd --sort=-priority

Last edited by MensaWater; 01-25-2017 at 02:58 PM.
 
1 members found this post helpful.
Old 01-24-2017, 06:43 PM   #3
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Thank you.

Quote:
Originally Posted by MensaWater View Post


EDIT:
On doing tests it appears you have to spell out "priority" for the ps built in sort to work (i.e. don't just use "pri":
Code:
ps -eo user,pid,pri,ni,cmd --sort=priority
Code:
ps -eo user,pid,pri,ni,cmd --sort=-priority

FROM PS MAN PAGE:
Quote:
--sort spec
Specify sorting order. Sorting syntax is [+|-]key[,[+|-]key[,...]]. Choose a multi-letter key from the STANDARD FORMAT SPECIFIERS section. The "+" is optional since default direction is increasing numerical or lexicographic order. Identical to k. For example: ps jax --sort=uid,-ppid,+pid
Then I looked a "STANDARD FORMAT SPECIFIERS" section and picked "pri" under CODE column which corresponds to "PRI" under HEADER column.

So where did you find specific instruction to use the word "priority" as sort key?

Last edited by fanoflq; 01-24-2017 at 07:41 PM.
 
Old 01-25-2017, 02:57 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,821
Blog Entries: 15

Rep: Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664
Quote:
Originally Posted by fanoflq View Post
So where did you find specific instruction to use the word "priority" as sort key?
I didn't. Being clever I knew "pri" was short for "priority" so decided to try that when I saw the same behavior (and man page) as you did.

My main reason for posting was to give you the other sort command's usage but was curious as to why the ps built in sort flag hadn't worked.

Glad I could help.

Please go to thread tools and mark this as Resolved so it will show as such in future searched.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting photorec output Mark_667 Linux - Software 3 05-25-2012 08:47 AM
Sorting GREP output UnixNewbie91 Linux - Newbie 1 04-28-2012 11:01 AM
[SOLVED] Sorting output of du. stf92 Linux - Newbie 5 03-30-2010 07:37 AM
Sorting data in output file lluciano Linux - General 2 08-19-2005 11:37 AM
Sorting of multi-column output rytrom Linux - Newbie 11 09-15-2003 12:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration