Article Title: multi-process viewing and configuration management in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
This section describes not only process viewing, but also what other users are doing because Linux is a multi-user system, therefore, this section also involves multiple users. At the same time, Linux is a multi-process system that often requires some deployment and management of these processes. To manage these processes, you must first know the current process: What processes are there? What is the process? And so on. Therefore, you need to view the process.
Who command
This command is used to view the current online users. This command is very useful. If you want to establish instant communication with other users, such as using the talk command, you must first make sure that the user is online. Otherwise, the talk process cannot be established. Another example is that the system administrator also needs to use the who command to monitor what every login user is doing at this moment.
The common syntax format of the who command is as follows:
Who [imqsuwHT] [-- count] [-- idle] [-- heading] [-- help] [-- message] [-- mesg] [-- version] [-- writable] [file] [am I]
All options are optional, that is, the who command can be used independently.
If no option is used, the who command displays the following three items:
Login name: logon username;
Terminal line: Use a terminal device;
Login time: the logon time to the system.
If two optional parameters are provided, the who command only displays the user name, logon terminal, and logon time of the who program. These two parameters are usually "am I", that is, the command format is "who am I ".
The following describes the common parameters of the who command.
-M is the same as "who am I" and displays the user name that runs the program.
-Q, -- count only displays the number of Logon accounts and users. This option takes precedence over other options.
-S ignore. It is mainly used for compatibility with who commands of other versions.
-I,-u, -- idle shows the last time the user has performed the system operation since the logon time, that is, the "Daze" time.
-H, -- heading displays the title of a row of columns. Common titles are shown in Table 4-2.
Table 4-2who command output common titles
Standard description
USER Logon account
LINE User Logon Terminal
LOGIN-TIME User Logon Time
IDLE user's IDLE time, that is, the time when the operation is not performed
PID process ID of the user logging on to the shell
FROM user network address
-W,-T -- mesg, -- message, -- writable is the same as the-s option. A character is displayed after the Logon account to indicate the user's information status:
+: Allowed to write information;
-: Information cannot be written;
? : The terminal device cannot be found.
-- Help displays help information on the standard output.
-- Version displays version information on the standard output.
The following describes some basic usage of the who command.
If you need to check which users are on the system, you can directly use the who command.
[Example 17] view logon users to the System
$ Who
Root tty1 Mar 17 :49
Foxy tty2 Mar 17 :49
Root tty3 Mar 17 :49
Bbs ttyp0 Mar 17 13:49 (river.net)
We can see that there are currently four users in the system. The first column is the logon user's account; the second column is the terminal used for Logon; the third column is the logon time; the fourth column is the network address where the user logs on, and the domain name.
In general, this gives you an overview of the login user. But sometimes the above display is not so intuitive, because there is no title description, it is not easy to understand, then you need to use the-H option.
[Example 18] to view the details of a logon user, type:
$ Who-uH
Shown as follows:
User line LOGIN-TIME (idle from)
Root tty1 Mar 17 :49.
Foxy tty2 Mar 17 01
Root tty3 Mar 17 01
Bbs ttyp0 Mar 17 (river.net)
This is clear. The "-u" option indicates that the user's IDLE time is displayed. Therefore, an IDLE is added. The IDLE item of the first root user is a ".", which means that the user is still active in the first second, and other users have a time later, called IDLE time.
Finally, let's take a look at the results of the command in the "who am I" format:
233.river.net! Root tty1 Mar 17 :49
Only the user running the who command is displayed. Of course, there is no idle time at this time.
The who command is very simple to use and can accurately grasp the user's situation, so it is widely used.
[NextPage]
W command
This command is also used to display the user login to the system. However, unlike who, the w command is more powerful. It not only shows who has logged on to the system, the current work of these users can also be displayed, and the statistical data is more detailed and scientific than the who command. We can think that the w command is an enhanced version of the who command.
The display items of the w command are arranged in the following order: current time, system start time, number of logged-on users, average load of the system in the last 1 s, 5 S, and 15 s. The data of each user is displayed in the following order: Logon account, terminal name, remote host name, Logon Time, idle time, JCPU, PCPU, and command line of the currently running process.
The JCPU time refers to the time occupied by all processes connected to the terminal (tty. This time does not include the previous background job time, but includes the time occupied by the currently running background job. The PCPU time refers to the time occupied by the current process (that is, the process displayed in WHAT. The following describes the specific usage and parameters of the command.
The syntax format is as follows:
W-[husfV] [user]
Parameters are described as follows:
-H does not display the title.
-U ignores the user name when listing the current process and CPU time. This is mainly used after the su command is executed.
-S uses the short mode. The logon time, JCPU, and PCPU time are not displayed.
-F: indicates the FROM entry, that is, the remote host name. The default value is that the remote host name is not displayed. Of course, the system administrator can make some modifications to the source file so that the displayed option becomes the default value.
-V displays the version information.
User Only displays information about the specified User.
[Example 19] displays the details of the user currently logged on to the system.
$ W
Pm up 2 min, 4 users, load average: 0.22, 0.16, 0.06
User tty from login @ IDLE JCPU PCPU WHAT
Root tty1 pm 0: 00 s 0.56 s 0.10 s w
Foxy tty2 pm 0.42 s 0.42 s bash
Root tty3 pm 46.00 s 0.67 s 0.25 s telnet bbs3
Bbs ttyp0 river.net pm 45.00 s 0.49 s 0.49 s bbs h river.net
Ps command
1. ps Overview
The two commands described above are used to view the current system user situation. The following describes the process situation, which is also the topic of this chapter. To monitor and control processes, you must first understand the current process, that is, you need to view the current process, and the ps command is the most basic and very powerful process viewing command. You can use this command to determine which processes are running and running, whether the process is terminated, whether the process is dead, and which processes are occupying excessive resources. In short, most of the information can be obtained by executing this command.
2. ps command and its parameters
The most common ps command is used to monitor the working conditions of background processes, because background processes do not communicate with standard input/output devices such as the screen keyboard, so if you need to detect the situation, you can use the ps command.
The syntax of this command is as follows:
Ps [Option]
The following describes the Command Options:
-E: displays all processes.
-F full format.
-H does not display the title.
-L long format.
-W width output.
A displays all processes on the terminal, including those of other users.
R only displays running processes.
X shows the process without any control terminal.
O [+ |-] k1 [, [+ |-] k2 [,…] The process list is displayed based on the multilevel sorting sequence specified by the shortcut KEYS in short keys, k1, and k2. The default sequence is specified for different ps formats. The default sequence can be overwritten by the user's specified sequence. The "+" character is optional, and the "-" character is used to reverse the direction of the specified key.
Pids only lists the conditions of the specified process. Process IDs are separated by commas. The process list must be followed by the last option of the command line parameter. spaces cannot be inserted in the middle. For example, ps-f1, 4, 5.
The following describes the long command line options, all of which start:
-- Sort X [+ |-] key [, [+ |-] key [,…] Select a multiple KEYS from the sort keys segment. The "+" character is optional, because the default direction is to sort the numbers in ascending order or the dictionary is responsible for 1 hot Shu? Ps-jax-sort = uid,-ppid, + pid.
-- Help: displays help information.
-- Version: displays the version information of the command.
The sort key is mentioned in the preceding option description, and further describes the sort key. Note that the values used in sorting are internal values used by ps, not only pseudo values in some output formats. The sorting Key List is shown in Table 4-3.
Table 4-3 sorting Key List
Short format
Long format
Description
C
Cmd
Simple executable name
C
Cmdline
Complete command line
F
Flags
Long MODE flag
G
Pgrp
Group ID of the process
G
Tpgid
Control tty Process Group ID
J
Cutime
Cumulative user time
J
Cstime
Accumulative system time
K
Utime
User time
K
Stime
System time
M
Min_flt
Number of secondary page errors
M
Maj_flt
Number of major page errors
N
Cmin_flt
Accumulative secondary page Error
N
Cmaj_flt
Accumulative Main Page Error
O
Session
Dialog ID
P
Pid
Process ID
P
Ppid
Parent process ID
R
Rss
Resident size
R
Resident
Resident page
S
Size
Memory size (kilobytes)
S
Share
Number of shared pages
T
Tty
Tty secondary device number
T
Start_time
Process start time
U
Uid
UID
U
User
User Name
V
Vsize
Total number of virtual memory (bytes)
Y
Priority
Kernel scheduling priority
[NextPage]
3. Common ps Command Parameters
The parameters described in the previous two sections may make readers feel a little scary. In fact, this is a very easy-to-use command. Generally, users only need to master some of the most common command parameters. The three most commonly used parameters are u, a, and x. The following uses an example to describe their usage.
[Example 20] log on to the system as root to view the current process status
$ Ps
PID TTY TIME COMMAND
5800 ttyp0 00:00:00 bash
5835 ttyp0 00:00:00 ps
The displayed items are divided into four parts: PID (process ID), TTY (terminal name), and TIME (process execution TIME) COMMAND (COMMAND line input of the process ).
You can use the u option to view the process owner and other details, as shown below:
$ Ps u
User pid % CPU % MEM USZ RSS TTY STAT START TIME COMMAND
Test 5800 0.0 0.4 1892 1040 ttyp0 S Nov27-bash
Test 5836 0.0 0.3 2528 856 ttyp0 R Nov27 ps u
There is a horizontal line in front of the bash process, which means that the process is the user's login shell, so for a login user, there is only one process with a short line. The options % CPU and % MEM are also displayed. The former indicates the percentage of CPU time and total time occupied by the process, and the latter indicates the percentage of memory and total memory occupied by the process.
In this case, we can see all the processes that control the terminal. However, we still haven't observed other processes that do not control the terminal. Therefore, we need to use the x option. Use the x option to view all processes.
[Example 21] the following example uses the x option:
$ Ps x
PID TTY STAT TIME COMMAND
5800 ttyp0 S-bash
5813 ttyp1 S-bash
5921 ttyp0 S man ps
5922 ttyp0 S sh-c/usr/bin/gunzip-c/var/catman/cat1/ps.1.gz |/
5923 ttyp0 S/usr/bin/gunzip-c/var/catman/cat1/ps.1.gz
5924 ttyp0 S/usr/bin/less-is
5941 ttyp1 R ps x
We can find that there are so many more processes at once. These extra processes are processes that do not control terminals. All the processes shown above are owned by the test user. In fact, many other users are using the system, which naturally corresponds to many other processes. If you want to know about these processes, you can use option a to view all processes of all users in the current system. The aux combination option is often used, which shows the most detailed process.
[Example 22]
$ Ps aux
User pid % CPU % MEM VSZ RSS TTY STAT START TIME COMMAND
Root 1 0.0 0.0 1136 64? S Nov25 0: 02 init [3
Root 2 0.0 0.0 0 0? SW Nov25 0: 00 [kflushd]
Root 3 0.0 0.0 0 0? SW Nov25 0: 03 [kupdate]
Root 4 0.0 0.0 0 0? SW Nov25 0: 00 [kpiod]
Root 5 0.0 0.0 0 0? SW Nov25 0: 00 [kswapd]
Root 163 0.0 0.1 1628 332? S Nov25 0: 02 sshd
Root 173 0.0 0.0 1324 200? S Nov25 0: 00 syslogd
Root 181 0.0 0.0 1420 0? SW Nov25 0: 00 [klogd]
Daemon 191 0.0 0.1 1160 312? S Nov25 0: 00/usr/sbin/
Root 201 0.0 0.1 1348 492? S Nov25 0: 00 crond
Root 212 0.0 0.0 1292 68? S Nov25 0: 00 inetd
......
At the top of the display is the process of other users. We can see users such as root and daemon and the processes they started. In the preceding example, we introduce some of the most common options and options for ps commands. You can select the options as needed.
Top Command
The top command and ps command have the same basic functions and display the current process and other conditions of the system. However, top is a dynamic display process, which allows you to refresh the current state by pressing the buttons. If you execute this command on the foreground, it excludes the foreground until the user terminates the program. More accurately, the top command provides real-time monitoring of the status of the system processor. It displays the list of CPU-most "sensitive" tasks in the system. This command can be used by CPU. The memory usage and execution time are used to sort tasks. Many features of this command can be set through interactive commands or in a custom file. The command parameters and interactive commands will be described separately in the subsequent sections.
The syntax format of the command is as follows:
Top [-] [d delay] [q] [c] [s] [S] [I]
D. Specify the interval between two screen information refreshes. Of course, you can use the s interactive command to change it.
Q This option will refresh top without any delay. If the caller has the superuser permission, top runs with the highest possible priority.
S indicates the accumulative mode.
S enables the top command to run in safe mode. This removes the potential danger of interactive commands.
I so that top does not show any idle or dead processes.
C. display the entire command line, not just the command name.
The top command displays many items. The default value is updated every 5 seconds. Of course, this can be set.
The displayed items are: uptime, which displays the system startup time, running time, and three average load values (load values for the last 1, 5, and 15 seconds ). Total number of processes running since the last refresh. Of course, these processes are divided into running, sleep, and stopped processes. Process and Status display can be achieved through interactive command t.
CPU states displays the percentage of CPU time used in various situations, such as user mode, system mode, priority process (only those with a negative priority), and idle. The time consumed by the priority process is also included in the user and system time, so the total percentage will be greater than 100%.
Mem memory usage statistics, including the total available memory, idle memory, used memory, shared memory and cache memory.
Swap space statistics, including the total Swap space, available Swap space, and used Swap space.
Pid id of each process.
PPID: the parent process ID of each process.
UID: The UID of each process owner.
USER: the username of each process owner.
The priority of each process in PRI.
The priority of the process.
SIZE: the code SIZE of the process, the data SIZE, and the total SIZE of the stack space. The Unit is KB.
TSIZE: the code size of the process. This is a strange value for Kernel processes.
DSIZE data and stack size.
TRS text resident size.
D. The page project marked as "not clean.
The size of the library page used by LIB. It does not work for the ELF process.
RSS the total number of physical memory occupied by the process, in KB.
SHARE the amount of memory used by the process.
STAT the status of the process.
S indicates the sleep state;
D indicates an uninterrupted sleep state;
R indicates the running status;
Z indicates the zombie state;
T indicates the stop or trace status.
[NextPage]
TIME the total cpu time occupied by the process since it was started. If you enter the accumulative mode, the time also includes the time occupied by the sub-process of the process. The title is changed to CTIME.
% CPU the percentage of CPU time and total time occupied by the process since the last refresh.
% MEM the percentage of physical memory occupied by the process to the total memory.
COMMAND indicates the COMMAND name of the process. If one line does not show any more, the COMMAND is intercepted. The process in the memory has a complete command line.
The following describes some interactive commands that can be used during top command execution. From the perspective of usage, mastering these commands is more important than mastering the options. These commands are single-letter. If the s option is used in the command line option, some of these commands may be blocked.
<空格> Refresh the display immediately.
Ctrl + L erase and override the screen.
H or? The help screen is displayed, and some brief command summary is provided.
K. terminate a process. The system prompts the user to enter the PID of the process to be terminated and the signal to be sent to the process. Generally, 15 signals can be used to terminate a process. If the process cannot end normally, use signal 9 to forcibly end the process. The default value is signal 15. This command is blocked in security mode.
I ignore idle and dead processes. This is a switch-on command.
Q: exit the program.
R reschedules the priority of a process. The system prompts the user to enter the process PID to be changed and the process priority value to be set. Entering a positive value will lower the priority, and vice versa will give the process a higher priority. The default value is 10.
S switches to the accumulative mode.
S changes the delay time between two refreshes. The system prompts the user to enter a new time in seconds. If there is a decimal number, it is converted to m s. If the input value is 0, the system will be refreshed continuously. The default value is 5 s. It should be noted that if the setting is too small, it is likely to cause constant refresh, so it is too late to see the display, and the system load will increase significantly.
F or F: add or delete a project from the current display.
O or O changes the order of projects displayed.
L switching displays average load and startup time information.
The m switch displays the memory information.
T Switch displays the process and CPU status information.
C switch to display the command name and complete command line.
M is sorted Based on the resident memory size.
P is sorted by CPU usage percentage.
T is sorted by time/accumulative time.
W write the current settings ~ /. Toprc file. This is a recommended method for writing top configuration files.
From the above introduction, we can see that the top command is a powerful tool for monitoring the system, especially for system administrators. Generally, users may think that the ps command is actually enough, but the powerful features of the top Command indeed provide a lot of convenience. Next let's take a look at the actual usage.
[Example 23] enter the top command to view the system status
$ Top
Pm up 7 min, 4 user, load average: 0.07, 0.09, 0.06
29 processes: 28 sleeping, 1 running, 0 zombie, 0 stopped
CPU states: 4.5% user, 3.6% system, 0.0% nice, 91.9% idle
Mem: 38916 K av, 18564 K used, 20352 K free, 11660 K shrd, 1220 K buff
Swap: 33228 K av, 0 K used, 33228 K free, 11820 K cached
Pid user pri ni size rss share stat lib % CPU % MEM TIME COMMAND
363 root 14 0 708 708 552 R 0 8.1 1.8 0: 00 top
1 root 0 0 404 404 344 S 0 0.0 0: 03 init
2 root 0 0 0 0 SW 0 0.0 0.0 kflushd
3 root-12-12 0 0 0 SW0 0.0 0.0 kswapd
4 root 0 0 0 0 SW 0 0.0 0.0 md_thread
5 root 0 0 0 0 SW 0 0.0 0.0 0: 00 md_thread
312 root 1 0 636 636 488 S 0 0.0 0: 00 telnet
285 root 6 0 1140 1140 804 S 0 0.0 2.9 bash
286 root 0 0 1048 1048 792 S 0 0.0 2.6 bash
25 root 0 0 364 364 312 S 0 0.0 0.9 kerneld
153 root 0 0 456 456 372 S 0 0.0 1.1 syslogd
160 root 0 0 552 552 344 S 0 0.0 1.4 klogd
169 daemon 0 0 416 416 340 S 0 0.0 1.0 atd
178 root 2 0 496 496 412 S 0 0.0 1.2 crond
187 bin 0 0 352 352 284 S 0 0.0 0.9 portmap
232 root 0 0 500 500 412 S 0 0.0 1.2 rpc. mountd
206 root 0 0 412 412 344 S 0 0.0 1.0 inetd
215 root 0 0 436 436 360 S 0 0.0 1.1 icmplog
The projects in the first line are the current time, system startup time, number of users logged on to the current system, and average load. The second behavior process is the total number of processes, the number of dormant processes, the number of running processes, the number of dead processes, and the number of terminated processes. The cpu status of the third action is user usage, system usage, priority process usage, and idle process usage. The fourth act is the memory status, which is the average available memory, used memory, idle memory, shared memory, and cache memory. The fifth behavior switching status is average available switching capacity, used capacity, idle capacity, and high-speed cache capacity. The following is a list of processes that are similar to ps.