Linux Bible. Christopher Negus
Чтение книги онлайн.
Читать онлайн книгу Linux Bible - Christopher Negus страница 62
To page through all processes running for all users on your system, use the ps aux
command as follows:
$ ps aux | less
A pipe (located above the backslash character on the keyboard) enables you to direct the output of one command to be the input of the next command. In this example, the output of the ps
command (a list of processes) is directed to the less
command, which enables you to page through that information. Use the spacebar to page through and type q to end the list. You can also use the arrow keys to move one line at a time through the output.
The ps
command can be customized to display selected columns of information and to sort information by one of those columns. Using the -o
option, you can use keywords to indicate the columns you want to list with ps
. For example, the next example lists every running process (-e
) and then follows the -o
option with every column of information I want to display, including
the process ID (pid
), username (user
), user ID (uid
), group name (group
), group ID (gid
), virtual memory allocated (vsz
), resident memory used (rss
), and the full command line that was run (comm
). By default, output is sorted by process ID number.
$ ps -eo pid,user,uid,group,gid,vsz,rss,comm | less PID USER UID GROUP GID VSZ RSS COMMAND 1 root 0 root 0 187660 13296 systemd 2 root 0 root 0 0 0 kthreadd
If you want to sort by a specific column, you can use the sort=
option. For example, to see which processes are using the most memory, I sort by the vsz field. That sorts from lowest memory use to highest. Because I want to see the highest ones first, I put a hyphen in front of that option to sort (sort=-vsz
).
$ ps -eo pid,user,group,gid,vsz,rss,comm --sort=-vsz | head PID USER GROUP GID VSZ RSS COMMAND 2366 chris chris 1000 3720060 317060 gnome-shell 1580 gdm gdm 42 3524304 205796 gnome-shell 3030 chris chris 1000 2456968 248340 firefox 3233 chris chris 1000 2314388 316252 Web Content
Refer to the ps man page for information on other columns of information by which you can display and sort.
Listing and changing processes with top
The top
command provides a screen-oriented means of displaying processes running on your system. With top
, the default is to display processes based on how much CPU time they are currently consuming. However, you can sort by other columns as well. After you identify a misbehaving process, you can also use top
to kill (completely end) or renice (reprioritize) that process.
If you want to be able to kill or renice any processes, you need to run top
as the root user. If you just want to display processes and possibly kill or change your own processes, you can do that as a regular user. Figure 6.1 shows an example of the top
window.
General information about your system appears at the top of the top
output, followed by information about each running process (or at least as many as will fit on your screen). At the top, you can see how long the system has been up, how many users are currently logged in to the system, and how much demand there has been on the system for the past 1, 5, and 10 minutes.
Other general information includes how many processes (tasks) are currently running, how much CPU is being used, and how much RAM and swap are available and being used. Following the general information are listings of each process, sorted by what percent of the CPU is being used by each process. All of this information is redisplayed every 5 seconds, by default.
FIGURE 6.1 Displaying running processes with top
The following list includes actions that you can do with top
to display information in different ways and modify running processes:
Press h to see help options, and then press any key to return to the top display.
Press M to sort by memory usage instead of CPU, and then press P to return to sorting by CPU.
Press the number 1 to toggle showing CPU usage of all your CPUs if you have more than one CPU on your system.
Press R to reverse sort your output.
Press u and enter a username to display processes only for a particular user.
A common practice is to use top
to find processes that are consuming too much memory or processing power and then act on those processes in some way. A process consuming too much CPU can be reniced to give it less priority to the processors. A process consuming too much memory can be killed. With top
running, here's how to renice or kill a process:
Renicing a process Note the process ID of the process you want to renice and press r. When the PID to renice message appears, type the process ID of the process you want to renice. When prompted to Renice PID to value, type in a number from –20 to 19. (See “Setting processor priority with nice and renice” later in this chapter for information on the meanings of different renice values.)
Killing a process Note the process ID of the process you want to kill and press k. Type 15 to terminate cleanly or 9 to just kill the process outright. (See “Killing processes with kill and killall” later in this chapter for more information on using different signals you can send to processes.)
Listing processes with System Monitor
If you have GNOME desktop available on your Linux system, System Monitor (gnome-system-monitor
) is available to provide a more graphical way of displaying processes on your system. You sort processes by clicking columns. You can right-click processes to stop, kill, or renice them.
To start System Monitor from the GNOME desktop, press the Windows key and then type System Monitor and press Enter. Then select the Processes tab. Figure 6.2 shows an example of the System Monitor window, displaying processes for the current user in order by memory use.
FIGURE 6.2 Use the System Monitor window to view and change running processes.
By default, only running processes associated with your user account are displayed. Those processes are listed alphabetically at first. You can resort the processes by clicking any of the field headings (forward and reverse). For example, click the %CPU heading to see which processes are consuming the most processing power. Click the Memory heading to see which processes consume the most memory.
You can change