Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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


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.