Linux Process Management and common operations

Basic introduction of process
  1. In LINUX, every program (code) executed is called a process. Each process is assigned an ID number.
  2. Each process corresponds to a parent process, which can copy multiple child processes. For example, www server.
  3. Each process can exist in two ways. Foreground and background, the so-called foreground process is that the user can operate on the current screen. The background process is actually operating, but because the process cannot be seen on the screen, it is usually executed in the background mode.
  4. Generally, the services of the system exist in the form of background process, and will reside in the system. It didn't end until it was turned off.
Displays the processes executed by the system explain

The instruction used for viewing is ps. generally speaking, the parameter used is ps -aux

ps -a: displays all process information of the current terminal

ps -u: display process information in user's format

ps-x: displays the parameters of the background process

[root@iZwz98zi7ua six three 8a one dxozhyZ ~]# ps
  PID TTY          TIME CMD
 one three six 0 pts/0    00:00:00 ps
 two one three 0 six pts/0    00:00:00 bash
[root@iZwz98zi7ua six three 8a one dxozhyZ ~]# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         one  0.0  0.0  four three five two 8  three 708 ?        Ss    two 0 one 9   7:0 one /usr/lib/systemd/systemd --system --deserialize two two
root         two  0.0  0.0      0     0 ?        S     two 0 one 9   0:00 [kthreadd]
root         three  0.0  0.0      0     0 ?        S     20 one 9   0:one 9 [ksoftirqd/0]
root         five  0.0  0.0      0     0 ?        S<    20 one 9   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S     2019   0:1 four [migration/0]
root         8  0.0  0.0      0     0 ?        S     2019   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        S     2019 1 five 2:20 [rcu_sched]
root        10  0.0  0.0      0     0 ?        S<    2019   0:00 [lru-add-drain]
root        11  0.0  0.0      0     0 ?        S     2019   2:five 1 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S     2019   2:12 [watchdog/1]
root        1 three  0.0  0.0      0     0 ?        S     2019   0:1 four [migration/1]
root        1 four  0.0  0.0      0     0 ?        S     2019   0:12 [ksoftirqd/1]
root        1 six  0.0  0.0      0     0 ?        S<    2019   0:00 [kworker/1:0H]
root        18  0.0  0.0      0     0 ?        S     2019   0:00 [kdevtmpfs]
root        19  0.0  0.0      0     0 ?        S<    2019   0:00 [netns]
root        20  0.0  0.0      0     0 ?        S     2019   0:17 [khungtaskd]
.
.
.

ps information options displayed:

fieldexplain
PIDProcess ID
TTYTerminal number
TIMECPU time consumed by this process
CMDThe name of the command or process being executed

Information options displayed by ps -aux:

fieldexplain
USERUser name
PIDProcess number
%CPUPercentage of CPU consumed by the process
%MEMPercentage of physical memory consumed by the process
VSZSize of virtual memory occupied by the process (unit: KB)
RSSSize of physical memory occupied by the process (unit: KB)
TTYTerminal name (abbreviation)
STATProcess status, where s - sleep, s - indicates that the process is the leader of the session, N - indicates that the process has a lower priority than the normal priority, R - running, D - short-term waiting, Z - dead process, T - tracked or stopped, etc
STARTStart time of the process
TIMECPU time, that is, the total time the process uses CPU
COMMANDThe commands and parameters used to start the process will be truncated if they are too long
example

1. Check whether the system starts the sshd service

[root@iZwz98zi7ua six 38a1dxozhyZ ~]# ps -aux | grep sshd
root      1109  0.0  0.0 1128 six four  2 five 80 ?        Ss    2019   0:33 /usr/sbin/sshd -D
root      98 five 0  0.0  0.0 112828   980 pts/0    D+   1 four:four 8   0:00 grep --color=auto sshd
root     2130 four  0.3  0.1 1 five 7 six 00  six 3 four 8 ?        Ss   1 four:29   0:0 four sshd: root@pts/0,pts/1
root     21321  0.0  0.1 1 five 727 six  five 944 ?        Ss   14:29   0:00 sshd: root@notty

2. Display all current processes in full format and view the parent process of the process

[root@iZwz98zi7ua six 38a1dxozhyZ ~]# ps -ef | more
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0  2019 ?        00:07:01 /usr/lib/systemd/systemd --system --deserialize 22
root         2     0  0  2019 ?        00:00:00 [kthreadd]
root         3     2  0  2019 ?        00:00:19 [ksoftirqd/0]
root         five     2  0  2019 ?        00:00:00 [kworker/0:0H]
root         7     2  0  2019 ?        00:00:14 [migration/0]
root         8     2  0  2019 ?        00:00:00 [rcu_bh]
root         9     2  0  2019 ?        02:32:21 [rcu_sched]
root        10     2  0  2019 ?        00:00:00 [lru-add-drain]
root        11     2  0  2019 ?        00:02:five 1 [watchdog/0]
root        12     2  0  2019 ?        00:02:12 [watchdog/1]
root        13     2  0  2019 ?        00:00:14 [migration/1]
root        14     2  0  2019 ?        00:00:12 [ksoftirqd/1]
root        16     2  0  2019 ?        00:00:00 [kworker/1:0H]
root        18     2  0  2019 ?        00:00:00 [kdevtmpfs]
root        19     2  0  2019 ?        00:00:00 [netns]
root        20     2  0  2019 ?        00:00:17 [khungtaskd]
root        21     2  0  2019 ?        00:00:00 [writeback]

ps -ef displays all current processes in full format:

  • -e displays all processes.
  • -f full format.

ps -ef displayed information options:

fieldexplain
UIDUser ID
PIDProcess ID
PPIDParent process ID
CThe factor used by the CPU to calculate the execution priority. The higher the value, the process is CPU intensive, and the execution priority will be reduced; A smaller value indicates that the process is I/O-Intensive, and the execution priority will increase
STIMETime the process started
TTYComplete terminal name
TIMECPU time
CMDCommands and parameters used to start the process

Terminate processes kill and kill

If a process needs to be stopped halfway, or when a large amount of system resources have been consumed, you can consider stopping the process at this time. Use the kill command to complete this task.

Kill [options] process number: kill the process through the process number (common options: - 9 means to force the process to stop immediately)
Kill process name: kill the process by the process name (wildcards are supported, which is useful when the system becomes very slow due to excessive load)

View process tree pstree

pstree [options]: you can view the process information more intuitively

Common options:

  • -p: Displays the PID of the process

  • -u: Displays the user to which the process belongs

[root@iZwz98zi7ua638a1dxozhyZ ~]# pstree -p
systemd(1)─┬─nginx(29129)─┬─nginx(1487)
           │              ├─nginx(1488)
           │              └─nginx(1489)
           ├─php-fpm(6152)─┬─php-fpm(3434)
           │               ├─php-fpm(3435)
           │               ├─php-fpm(3436)
           │               ├─php-fpm(3439)
           │               ├─php-fpm(3440)
           │               ├─php-fpm(3441)
           │               ├─php-fpm(5370)
           │               ├─php-fpm(6154)
           │               ├─php-fpm(6155)
           │               ├─php-fpm(6156)
           │               └─php-fpm(26163)
           ├─polkitd(594)─┬─{polkitd}(671)
           │              ├─{polkitd}(672)
           │              ├─{polkitd}(673)
           │              ├─{polkitd}(674)
           │              ├─{polkitd}(675)
           │              └─{polkitd}(679)
           ├─pure-ftpd(5883)
           ├─redis-server(22094)─┬─{redis-server}(22095)
           │                     ├─{redis-server}(22096)
           │                     ├─{redis-server}(22097)
           │                     └─{redis-server}(22098)
           ├─redis-server(22100)─┬─{redis-server}(22101)
           │                     ├─{redis-server}(22102)
           │                     ├─{redis-server}(22103)
           │                     └─{redis-server}(22104)
           ├─redis-server(22106)─┬─{redis-server}(22107)
           │                     ├─{redis-server}(22108)
           │                     ├─{redis-server}(22109)
           │                     └─{redis-server}(22110)
           ├─redis-server(22126)─┬─{redis-server}(22127)
           │                     ├─{redis-server}(22128)
           │                     ├─{redis-server}(22129)
           │                     └─{redis-server}(22130)
           ├─redis-server(22132)─┬─{redis-server}(22133)
           │                     ├─{redis-server}(22134)
           │                     ├─{redis-server}(22135)
           │                     └─{redis-server}(22136)
           ├─redis-server(22196)─┬─{redis-server}(22197)
           │                     ├─{redis-server}(22198)
           │                     ├─{redis-server}(22199)
           │                     ├─{redis-server}(22200)
           │                     └─{redis-server}(22202)
           ├─rsyslogd(902)─┬─{rsyslogd}(936)
           │               └─{rsyslogd}(955)
           ├─sshd(1109)─┬─sshd(21304)─┬─bash(21306)───pstree(23878)
           │            │             ├─bash(21322)───top(21436)
           │            │             └─bash(23851)───sleep(23860)
           │            └─sshd(21321)─┬─sftp-server(21338)
           │                          ├─sftp-server(21345)
           │                          ├─sftp-server(21352)
           │                          ├─sftp-server(21359)
           │                          ├─sftp-server(21366)
           │                          └─sftp-server(21373)
           ├─systemd-journal(396)
           ├─systemd-logind(593)
           ├─systemd-udevd(429)
           └─tuned(897)─┬─{tuned}(1012)
                        ├─{tuned}(1013)
                        ├─{tuned}(1014)
                        └─{tuned}(1028)

Service management

A service is essentially a process, but it runs in the background. It usually listens to a port and waits for requests from other programs, such as mysql, sshd, firewall, etc. Therefore, it is also called a daemon.

service management instruction

service service name [start | stop | restart | reload | status]

Note: after CentOS 7.0, service is no longer used, but systemctl

Use example

View firewall status: service iptables status

Open firewall: service iptables start

Close the firewall: service iptables stop

Note: this method is only a temporary shutdown and will become invalid after restart. To make it permanent, use the chkconfig command.

View service name

Method 1: use setup - > system service

Mode 2: / etc/init.d / service name

[root@iZwz98zi7ua638a1dxozhyZ ~]# ls -l /etc/init.d/
Total dosage 80
-rwxr-xr-x  1 root root  2415 3 June 30 00:39 aegis
-rwxr-xr-x  1 root root  9873 9 December 21:34 bt
-rw-r--r--. 1 root root 18281 8 June 24, 2018 functions
-rwxr-xr-x  1 root root 10642 11 August 2019 mysqld
-rwxr-xr-x. 1 root root  4569 8 June 24, 2018 netconsole
-rwxr-xr-x. 1 root root  7923 8 June 24, 2018 network
-rwxrwxr-x  1 root root  2753 5 June 28, 2019 nginx
-rwxr-xr-x  1 root root  2405 5 June 2019 php-fpm-73
-rwxr-xr-x  1 root root  1447 8 June 17, 2016 pure-ftpd
-rw-r--r--  1 root root  1160 7 March 2019 README

Service runlevel

View or modify the default level: vi /etc/inittab

Linux systems have seven runlevels: commonly used are levels 3 and 5

  • Operation level 0: the system is in shutdown status. The system default operation level cannot be set to 0, otherwise it cannot be started normally
  • Operation level 1: single user working status, root permission, used for system maintenance, and remote login is prohibited
  • Runlevel 2: multi user state (no NFS), no network support
  • Run level 3: full multi-user status (with NFS). After logging in, enter the console command line mode
  • Run level 4: system not used, reserved
  • Operation level 5: X11 console, log in and enter the graphical GUI mode
  • Operation level 6: the system is shut down and restarted normally. The default operation level cannot be set to 6, otherwise it cannot be started normally

Process description of startup:

chkconfig instruction

The chkconfig command allows you to set the self start / shutdown for each run level of each service

View service: chkconfig [service name] - list | grep xxx

[root@iZwz98zi7ua638a1dxozhyZ ~]# chkconfig --list
aegis           0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut
bt              0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut
mysqld          0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut
netconsole      0:shut    1:shut    2:shut    3:shut    4:shut    5:shut    6:shut
network         0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut
nginx           0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut
php-fpm-73      0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut
pure-ftpd       0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut
[root@iZwz98zi7ua638a1dxozhyZ ~]# chkconfig --list | grep nginx
nginx           0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut
[root@iZwz98zi7ua638a1dxozhyZ ~]# chkconfig nginx --list
nginx           0:shut    1:shut    2:open    3:open    4:open    5:open    6:shut

Modify service: chkconfig --level service name on/off

[root@iZwz98zi7ua638a1dxozhyZ ~]# chkconfig --level 5 nginx off
[root@iZwz98zi7ua638a1dxozhyZ ~]# chkconfig --list | grep nginx
nginx           0:shut    1:shut    2:open    3:open    4:open    5:shut    6:shut

Note: after chkconfig resets the service, it starts or shuts down automatically. It takes effect only after restarting the machine and reboot ing

Dynamic monitoring process

Top is similar to the ps command. They are used to show the process being executed. The biggest difference between top and ps is that top can update the running process after a period of execution.

top [option]
optionfunction
-d secondsSpecifies that the top command is updated every few seconds. The default is 3 seconds. Commands that can be executed in the interactive mode of the top command
-iMake top not show any idle or dead processes
-pMonitor the status of only one process by specifying the monitoring process ID

Interactive operation function description:

operationfunction
PSort by CPU utilization. This item is selected by default
MSort by memory usage
NSort by PID
qExit top

View system network

netstat [option]

Option Description:

-an arranges the output in a certain order

-p shows which process is calling

[root@iZwz98zi7ua638a1dxozhyZ ~]# netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:16389         0.0.0.0:*               LISTEN      22094/redis-server  
tcp        0      0 127.0.0.1:16390         0.0.0.0:*               LISTEN      22100/redis-server  
tcp        0      0 127.0.0.1:16391         0.0.0.0:*               LISTEN      22106/redis-server  
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      22196/redis-server  
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      22126/redis-server  
tcp        0      0 127.0.0.1:6381          0.0.0.0:*               LISTEN      22132/redis-server  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1487/nginx: worker  
tcp        0      0 127.0.0.1:6389          0.0.0.0:*               LISTEN      22094/redis-server  
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      5883/pure-ftpd (SER 
.
.
.

Tags: Linux Operation & Maintenance

Posted on Sat, 25 Sep 2021 14:53:18 -0400 by jalapena