Книга: Fedora™ Unleashed, 2008 edition
Listing Processes with ps
Listing Processes with ps
This is the third and last "command that should be simple, but isn't" that is discussed here. The ps command lists processes and gives you an extraordinary amount of control over its operation.
The first thing to know is that ps is typically used with what are known as BSD-style parameters. Back in the section "Finding Files by Searching with find
," we discussed UNIX-style, GNU-style, and X-style parameters (-c
, --dosomething
, and -dosomething
, respectively), but BSD-style parameters are different because they use single letters without a dash.
The default use of ps therefore lists all processes you are running that are attached to the terminal. However, you can ask it to list all your processes attached to any terminal (or indeed no terminal) by adding the x
parameter: ps x. You can ask it to list all processes for all users with the a
parameter or combine that with x
to list all processes for all users, attached to a terminal or otherwise: ps ax
.
However, both of these are timid compared with the almighty u
option, which enables user-oriented output. In practice, that makes a huge difference because you get important fields such as the username of the owner, how much CPU time and RAM are being used, when the process was started, and more. This outputs a lot of information, so you might want to try adding the f
parameter, which creates a process forest by using ASCII art to connect parent commands with their children. You can combine all the options so far with this command: ps faux
(yes, with a little imagination, you spell words with the parameters!).
You can control the order in which the data is returned by using the --sort
parameter. This takes either a +
or a -
(although the +
is default) followed by the field by which you want to sort: command
, %cpu
, pid
, and user
are all popular options. If you use the minus sign, the results are reversed. This next command lists all processes, in descending order by CPU use:
$ ps aux --sort=-%cpu
There are many other parameters for ps, including a huge number of options for compatibility with other UNIXes. If you have the time to read the man page, you should give it a try!
- Printing the Contents of a File with cat
- Changing Directories with cd
- Changing File Access Permissions with chmod
- Copying Files with cp
- Printing Disk Use with du
- Finding Files by Searching with find
- Searches for a String in Input with grep
- Paging Through Output with less
- Creating Links Between Files with ln
- Finding Files from an Index with locate
- Listing Files in the Current Directory with ls
- Reading Manual Pages with man
- Making Directories with mkdir
- Moving Files with mv
- Listing Processes with ps
- Deleting Files and Directories with rm
- Printing the Last Lines of a File with tail
- Printing Resource Usage with top
- Printing the Location of a Command with which
- Moving Files with mv
- Printing the Location of a Command with which
- 13.4.4. ps
- 15.4.1. Debugging Multiple Processes
- Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
- Drawbacks with restore
- Listing your active rule-set
- 7. AGGREGATION WITH INDEPENDENT WORKS
- Using the kill Command to Control Processes
- Конструкция with-do
- 3. Hexadecimal – the way we communicate with micros
- CHAPTER 3 Working with GNOME