Linux process and service management

1. Basic introduction of the process

  • In LINUX, each executed program (code) is called a process. Each process is assigned an ID number.
  • Each process corresponds to a parent process, and this parent process can replicate multiple child processes.
  • Each process may exist in two ways. Foreground and background, the so-called foreground process is what 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.
  • Generally, system services exist in the form of background processes, and they will always reside in the system. It didn't end until shut down.

Two, commonly used commands

ps命令  用来显示进程的状态通常会配合管道符一起使用
ps [选项]
常用选项
-a 显示当前终端的所有进程信息
-u 以用户的格式显示进程信息
-x 显示后台进程运行的参数
-e 显示所有进程
-f 全格式,显示UID,PPIP,C与STIME栏位
 
# 常用组合
ps -aux 
ps -ef

2.1, ps -aux command

image-20200914224530155

  • USER : user name
  • PID : process number
  • %CPU : The percentage of CPU occupied by the process
  • %MEM : the percentage of physical memory occupied by the process
  • VSZ : The virtual memory size occupied by the process (unit: KB )
  • RSS : The size of physical memory occupied by the process (unit: KB )
  • TTY : Abbreviation of terminal name
  • STAT : process status, where S-sleeping, s-means that the process is the leading process of the session, N-means that the process has a lower priority than normal priority, R-is running, D-short-term waiting, Z-dead process , T-be tracked or stopped, etc.
  • START : the start time of the process
  • TIME : CPU time, that is, the total time the process uses the CPU
  • COMMAND : The command and parameters used to start the process

2.2, ps -ef command

ps -ef # 以全格式显示当前所有的进程,查看进程的父进程。
-e 显示所有进程
-f 全格式,显示UID,PPIP,C与STIME栏位

image-20200915144746168

  • UID : User ID
  • PID : Process ID
  • PPID : parent process ID
  • C : The factor used by the CPU to calculate the execution priority. The larger the value, it indicates that the process is a CPU-intensive operation, and its execution priority will decrease; the smaller the value, it indicates that the process is an I/O-intensive operation, and its execution priority will increase.
  • STIME : the time when the process started
  • TTY : full terminal name
  • TIME : CPU time
  • CMD : Commands and parameters used to start the process

ps -ef and ps -aux are displayed in two different styles, -ef is displayed in BSD style, and -aux is displayed in System V style

2.3, terminate the process kill and kill all

  1. Kill, as the word means, is to kill. The kill command in linux system is used to delete the program or job in execution. The kill command can send the specified signal to the corresponding process or job. The kill command uses a signal of 15 by default to end the process or work. If the process or job ignores this signal, you can use signal 9 to forcefully kill the process or job.
kill [选项] [进程号] #杀死进程
常用选项
-l	列出系统支持的信号
-s	指定向进程发送的信号
-a	处理当前进程时不限制命令名和进程号的对应关系
-p	指定kill命令只打印相关进程的进程号,而不发送任何信号
# 强制杀死进程
kill -9 [进程号]
  1. The killall command uses the name of the process to kill the process. Use this command to kill a group of processes with the same name.
kill [选项] [进程名称]
常用选项
-e	对长名称进行精确匹配
-l	打印所有已知信号列表
-p	杀死进程所属的进程组
-i	交互式杀死进程,杀死进程前需要进行确认
-r	使用正规表达式匹配要杀死的进程名称
-s	用指定的进程号代替默认信号“SIGTERM”
-u	杀死指定用户的进程
  1. The pstree command, the full English name of the pstree command in the Linux system is "process tree", that is to say, all the itineraries will be displayed in a tree diagram, and the tree diagram will be based on the pid (if specified) or the init basic itinerary as the root (root) , If there is a specified user id, the tree view will only show the itinerary owned by the user.

    #命令
    pstree [选项]
    常用选项
    -p # 显示当前所有进程的进程号和进程id
    -u # 显示进程的所属用户
    -a #显示每个程序的完整指令,包含路径,参数或是常驻服务的标示
    -h #列出树状图时,特别标明现在执行的程序
    

Three, top command to dynamically view the process

top [选项]
常用选项
-d # 改变显示的更新速度 或者在交互模式下按s
-c # 显示完整的进程信息
-n # 更新的次数,完成后将会退出 top
-i # 不显示任何闲置 (idle) 或无用 (zombie) 的行程
-p # 通过制定监控ID来仅仅监控某个进程的状态

image-20200915202522961

Interactive mode operation instructions:

image-20200915203024515

u: View the process of the specified user

k: kill the process you want to kill

Four, netstat view system network conditions

The netstat command is used to display various network-related information, such as network connections, routing tables, interface status (Interface Statistics), masquerade connections, Multicast Memberships, and so on. On the whole, the output of netstat can be divided into two parts: One is Active Internet connections, called active TCP connections, where "Recv-Q" and "Send-Q" refer to %0A for receiving queue and sending queue. These numbers should generally be 0. If it is not, it means that the package is accumulating in the queue. This situation can only be seen in very rare cases; the other is Active UNIX domain sockets, called active Unix domain sockets (same as network sockets, but can only be used for local communication, performance can be improved by one Times).

Active Internet connections

image-20200915204123561

Active UNIX domain sockets

image-20200915204159783

#netstat命令
-a #显示所有连线中的Socket
-p #显示正在使用Socket的程序识别码和程序名称
-u #显示UDP传输协议的连线状况
-i #显示网络界面信息表单
-n #直接使用IP地址,不通过域名服务器
#显示UDP端口号和使用情况
net -apu

image-20200915204655104

# 显示网卡列表
netstat -i

image-20200915204803396

Five, service management

​ Service (service) is essentially a process, but it runs in the background. It usually listens on a port and waits for requests from other programs, such as (mysql, sshd firewall, etc.), so we are also called a daemon. For service management instructions, the command service is usually used before centos7. Service is no longer used after CentOS7.0, but systemctl.

​ After Centos7, init was completely replaced with systemd startup mode. The systemd startup service mechanism is mainly handled by this system service management command of systemctl. Systemctl also covers most of the functions of service / chkconfig / setup / init in usage.

#命令格式 
systemctl [选项] [服务]
常用选项
-start	# 启动服务
-stop	# 停止服务
-restart	# 重启服务
-enable	# 使某服务开机自启
-disable	# 关闭某服务开机自启
-status	# 查看服务状态
systemctl list-units --type=service # 列举所有已启动服务(unit单元) 

Common commands for services

# 查看ip地址
ifconfig

# 查看防火墙的状态
systemctl list-unit-files|grep firewalld.service 
systemctl status firewalld.service

#关闭防火墙
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已经启动的服务:systemctl list-unit-files|grep enabled

查看防火墙开放的端口:firewall-cmd --list-ports
开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效

Reference link: https://www.cnblogs.com/programmer-tlh/p/11593330.html

Guess you like

Origin blog.csdn.net/qq_44134480/article/details/108609663