Viewing Processes
Linux is a multi-task system and needs to get process information during process management. Multiple commands are available to view processes.
who Command
The who command is used to display system user information. For example, before running the talk command to establish instant communication with another user, you need to run the who command to determine whether the target user is online. As another example, the system administrator can run the who command to learn what each login user is doing at the current time. The who command is widely seen in system administration since it is easy to use and can return a comprehensive set of accurate user information.
The following is an example output of the who command, where system users and their status are displayed:
# who admin tty1 Jul 28 15:55 admin pts/0 Aug 5 15:46 (9.1.0.110) admin pts/2 Jul 29 19:52 (9.1.0.110) root pts/3 Jul 30 12:07 (9.1.0.110) root pts/4 Jul 31 10:29 (9.1.0.144) root pts/5 Jul 31 14:52 (9.1.0.11) root pts/6 Aug 6 10:12 (9.1.0.234) root pts/8 Aug 6 11:34 (9.1.0.234)
ps Command
The ps command is used to display process information, including which processes are running, terminated, resource-hungry, or stay as zombies.
A common scenario is using the ps command to monitor background processes, which do not interact with your screen, keyboard, and other I/O devices. Table 6-2 lists the common ps command options.
Option |
Description |
---|---|
-e |
Displays all processes. |
-f |
Full output format. |
-h |
Hides column headings in the listing of process information. |
-l |
Long output format. |
-w |
Wide output format. |
-a |
Lists all processes on a terminal, including those of other users. |
-r |
Lists only running processes. |
-x |
Lists all processes without controlling terminals. |
For example, to list all processes on a terminal, run the following command:
# ps -a PID TTY TIME CMD 12175 pts/6 00:00:00 bash 24526 pts/0 00:00:00 vsftpd 29478 pts/5 00:00:00 ps 32461 pts/0 1-01:58:33 sh
top Command
Both the top and the ps commands can display a list of currently running processes, but the top command allows you to update the displayed list of processes repeatedly with the press of a button. If the top command is executed in foreground, it exclusively occupies foreground until it is terminated.
The top command provides real-time visibility into system processor status. You can sort the list of CPU tasks by CPU usage, memory usage, or task execution time. Extensive customization of the display, such as choice of columns or sorting method, can be achieved using interactive commands or the customization file.
Figure 6-1 provides an example output of the top command.
kill Command
The kill command is used to terminate a process regardless of whether the process is running in foreground or background. It differs from the combo key Ctrl+c, which can terminate only foreground processes. The reason for terminating a background process can be heavy use of CPU resources or deadlock.
The kill command sends a signal to terminate running processes. By default, the TERM signal is used. The TERM signal terminates all processes incapable of capturing the TERM signal. To terminate a process capable of capturing the TERM signal, use the KILL signal (signal ID: 9) instead.
Two types of syntax of the kill command:
kill [-s signal | -p] [-a] PID… kill -l [signal]
The process ID is retrieved from the ps command. The -s option indicates the signal sent to terminate processes. The -p option indicates the ID of process that will be terminated.
For example, to terminate the process with ID 1409, run the following command:
# kill -9 1409
Example output of the kill command with the -l option
# kill -l 1) SIGHUP? 2) SIGINT? 3) SIGQUIT? 4) SIGILL 5) SIGTRAP? 6) SIGABRT? 7) SIGBUS? 8) SIGFPE 9) SIGKILL?10) SIGUSR1?11) SIGSEGV?12) SIGUSR2 13) SIGPIPE?14) SIGALRM?15) SIGTERM?16) SIGSTKFLT 17) SIGCHLD?18) SIGCONT?19) SIGSTOP?20) SIGTSTP 21) SIGTTIN?22) SIGTTOU?23) SIGURG?24) SIGXCPU 25) SIGXFSZ?26) SIGVTALRM?27) SIGPROF?28) SIGWINCH 29) SIGIO?30) SIGPWR?31) SIGSYS?34) SIGRTMIN 35) SIGRTMIN+1?36) SIGRTMIN+2?37) SIGRTMIN+3?38) SIGRTMIN+4 39) SIGRTMIN+5?40) SIGRTMIN+6?41) SIGRTMIN+7?42) SIGRTMIN+8 43) SIGRTMIN+9?44) SIGRTMIN+10?45) SIGRTMIN+11?46) SIGRTMIN+12 47) SIGRTMIN+13?48) SIGRTMIN+14?49) SIGRTMIN+15?50) SIGRTMAX-14 51) SIGRTMAX-13?52) SIGRTMAX-12?53) SIGRTMAX-11?54) SIGRTMAX-10 55) SIGRTMAX-9?56) SIGRTMAX-8?57) SIGRTMAX-7?58) SIGRTMAX-6 59) SIGRTMAX-5?60) SIGRTMAX-4?61) SIGRTMAX-3?62) SIGRTMAX-2