Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there any way to get the PID of the top CPU/MEM usage, I need to use that in a script, top command doesn't seem to work for it.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

ps has many options for displaying and sorting details about processes.

e.g. ps hax o pid k -time | head -n 1 will give you the PID of the process with the most CPU time used.

See the man page for full details. The man page functions well enough as a reference but is not a tutorial - IMO your best option is to read and experiment until you understand what it can do and how to use it best. AFAIK, there is no tutorial-style documenation for ps available anywhere.

share|improve this answer

Try something like this:

top -b -n1 | tail -n+8 | sort -nr -k9
share|improve this answer
    
+1 for introducing batch mode –  warl0ck Sep 19 '12 at 8:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.