Linux is a multi-user system, and sometimes you need to know what other users are doing. Therefore, this section also covers multi-user content. 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 mainly used to view the current online user information. 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 to say, the who command can be used separately. If you do not use any options, the who command displays the following three items: login name: logon username, terminal line: terminal device, and login time: the time when you log on 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 and count only display 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, and idle display the last time the user has performed system operations since the logon time, that is, the "Daze" time. The "." symbol indicates that the user is still active in the first second. the "old" symbol indicates that the user has been idle for more than 24 hours. -H, heading displays the title of a row of columns. Common titles are shown in Table 4-2.
Who command output common title
USER Login Account line user login using terminal LOGIN-TIME USER login time idle user idle alarm lip threats running? The PID user logs on to the shell process id from the user's network address-w,-Tmesg, message, writable and-s options. A character is displayed after the Logon account to indicate the user's information status: +: allowed to write information;-: not allowed to write information ;? : 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 1] view logon users to the System
$ Who root tty1 Mar 17 foxy tty2 Mar 17 49 root tty3 Mar 17 bbs ttyp0 Mar 17 river.net) We can see that there are currently four users in the system. 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 2] view logon user details,
Type: $ who-uH: user line LOGIN-TIME idle from root tty1 Mar 17. foxy tty2 Mar 17 01 root tty3 Mar 17 01 bbs ttyp0 Mar 17 river.net. 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 result of the command in the "who am I" Format: 233.river.net! Root tty1 Mar 17 only shows the user running the who command, 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. W command this command is also used to display the user login to the system, but unlike who, w command is more powerful, it not only shows who 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 items. The following describes the specific usage and parameters of the command. The syntax format is as follows: w-[husfV] [user] describes the parameters:-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 3] displays the details of the users 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 telnet bbs3 bbs ttyp0 river.net pm 0.25 s 45.00 s 0.49 s bbs h river.net ps command 1. the two commands described earlier in ps overview are used to view the current user status of the system. The following describes the process status, 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. the ps command and Its Parameter ps command are most commonly 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, therefore, if you need to check the situation, you can use the ps command. The syntax format of this command is as follows:-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 values marked as "8 then" and 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 long command line options. These options start with "": sort X [+ |-] key [, [+ |-] key [,…] Select a multiple KEYS from the sort keys segment. The "+" character is optional, because the default direction is in ascending order of numbers or dictionary order. For example, 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 for some output formats.
Sort key list short format long format description
C cmd executable simple name C cmdline complete command line f flags Long MODE Mark g pgrp Process Group id g tpgid control tty Process Group ID j cutime cumulative user time J cstime cumulative system time k utime user time K stime system time m min_flt Number of secondary page errors M maj_flt Number of primary page errors n cmin_flt cumulative secondary page error N cmaj_flt cumulative primary page error o session dialog ID p pid process id p ppid parent process ID r rss resident size R resident page s size memory size kilobytes) number of S share sharing pages t tty secondary device No. T start_time process start time U uid u user name v vsize total number of virtual memory bytes) y priority kernel Scheduling Level 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 commonly used command parameters. The three most commonly used parameters are u, a, and x. The following uses an example to describe their usage.
[Example 4] log on to the system as a root user
, View the current process Status $ ps pid tty time command 5800 ttyp0 00:00:00 bash 5835 ttyp0 00:00:00 ps can be seen, the displayed items are divided into four, in turn, PID process ID), TTY terminal name), TIME process execution TIME), 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 ttyp0 R Nov27 ps u there is a horizontal line in front of the bash process, this 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.