60 seconds about psPosted: June 24, 2007
If you use Unix, you’ve probably seen ps once or twice. Its one of the most useful tools in Unix. I use it to check if Oracle is up or not (by looking for smon process), check if a process I killed is dead or not, look for the parent of a process that is taking too much CPU, etc, etc.
I always knew that this tool has more options that just the good old ps -ef, but only today I found the time to really read the man page and see what they do. I found some cool stuff I want to share.
First, the selectors – you can ask ps to show just the processes that belong to a specific user (say, oracle) by running “ps -u oracle” will show all processes that belong to oracle.
There is also an option to select by command name, but it doesn’t work as expected, the command name is not what ps itself will show in the CMD column. So ps -fC oracle will show ora_smon_SID while ps -fC ora_smon_SID will show nothing.
Then, you can ask ps to show you certain columns:
ps -eo pid,ppid,pcpu,pmem,etime,time,comm,args will show you the process id, parent process if, cpu utilization, memory utilization, elapsed time, cpu time, command line and arguments.
And the last cool feature is sorting: you can sort by start time, user time, system time, page fault, user id and other useful parameters.
ps -ef –sort utime will show all the processes sorted by user time.
And combining everything I learned today, I can run:
ps -C oracle –sort cutime -o pid,ppid,pcpu,pmem,etime,time,comm,args
and find out that the oracle process that is taking most user time is ora_lms0_SID, but thats probably because I’m checking this on a rather unloaded system.
Key information is missing from the ps man page – it does not contain the list of parameters that –sort and -o (list of columns) will take. I found the lists here: http://www.ss64.com/bash/ps_keywords.html but I have to assume they exist on some man page, otherwise it is huge neglect by Unix documentation.