Linux process management
basic introduction
In Linux, each executed program is called a process, and each process is assigned an ID number (PID)
Each process may exist in two ways, foreground and background
Generally, the services of the system exist in the form of background processes, and they will be resident in the system until the system is shut down.
Display the processes executed by the system
Basic introduction to ps command
ps (process show) is used to display what is currently being executed in the system and their execution status.
ps [选项]
optional options
- ps -a displays all process information of the current terminal
- ps -u displays process information in user format
- ps -x displays the parameters of the background process running
Generally speaking, the combined use is:ps -aux
- ps -e: show all current processes
- ps -f: full format
Terminate process kill and killall
basic grammar
kill [选项] 进程号
- Kill/terminate process by process ID
- options
-9
Indicates to force the process to stop immediately
killall 进程名称
- kill process by process name
View process tree pstree
yum install psmisc
❗️: If it shows that there is no such command, you can install this command in cenos .
Basic syntax:pstree [选项]
You can view process information more intuitively
Common options
- -p displays the pid of the process
- -u displays the user the process belongs to
service service management
service introduction
The essence of a service (service) is a process, but the process runs in the background. It usually listens to a certain port and waits for requests from other programs.
Therefore, daemons are also called daemons.
service management command
service 服务名 [start|stop|restart|status]
- After cenos7, many services no longer use service to manage, but use systemctl
- ⭐️The services managed by the service command
/etc/init.d
are viewed in ([[1-Linux directory structure #^f9a9d2|linux /etc directory]])
chkconfig command
Through the chkconfig command, you can set self-start/shutdown for each run level of the service
In other words, the self-starting and closing of the service is for different machine operation levels
Display the services supported by the chkconfig command:chkconfig --list
Use the chkconfig command
Format:chkconfig --level 级别 服务名 on/off
![[3-Linux practical operation#specified run level]]
❗️: When using chkconfig to reset the self-start and shutdown of the service at different levels, the machine needs to be restarted to take effect.
systemctl management commands
basic instructions
systemctl [start|stop|restart|status] 服务名
⭐️: The services managed by the systemctl/usr/lib/systemd/system
command are viewed in
![[1-Linux directory structure #^050dfd]]
systemctl sets the self-starting state of the service
- View service startup status
systemctl list-unit-files
- Set the service to start at boot
systemctl enable 服务名
- Close the service and start it
systemctl disable 服务名
- Query whether a service is self-starting
systemctl is-enabled 服务名
example
Check the firewalld service supported by systemctl
It is found that firewalld.service exists
Then you can control and view the
systemctl status firewalld
systemctl stop firewalld
systemctl start firewalld
Open or close the specified port
- open port
firewall-cmd --permanent --add-port=端口号/协议
- close port
firewall-cmd --permanent --add-port=端口号/协议
- Reload to take effect
firewall-cmd --reload
- Query whether the port is open
firewall-cmd --query-port=端口/协议
In win, you can telnet IP地址 端口
test whether the port of a certain host is open by
Dynamic monitoring process
basic usage
top [选项]
Both "top" and "ps" are commands used in Unix and Unix-like systems to view information about processes running on the system. The difference between them:
- Function
- top is a dynamic process monitoring tool, which displays real-time process information running in the system , including CPU usage, memory usage, etc.
- ps is a static process viewing tool, which displays the process information in the current system at one time and will not be updated in real time.
- Display method
- top displays process information interactively, dynamically sorts and filters processes, and updates the display in real time.
- ps displays process information on the command line, and you need to use different options to specify the information to be displayed.
Option Description
-d 秒数
: Specify the top command to update every few seconds, the default is 3s-i
: use top to not show any idle or zombie processes-p
: Only monitor the status of a process by specifying the monitoring process ID
Interactive operation in top
operate | Function |
---|---|
P | Sort by CPU usage, the default is this option |
M | Sort by memory usage |
N | Sort by PID |
u | Enter a username to monitor a specific user |
k | Enter the ID number of the process to be terminated to terminate the specified process |
q | quit |
Check network status
View system network status netstat
- basic grammar
netstat [选项]
- View system network status
- Option Description
-an
Arrange the output in a certain order-p
Show which process is calling
Detect host connection command ping
Check whether the connection to the remote host is normal