处理 Management
Monitor, control, and manage 运行处理 using ps, 顶部, kill, and service managers.
你将学到
- 什么处理是 and 如何 OS manages 它们
- Listing and inspecting 处理使用 ps and 顶部
- 发送 signals 到处理使用 kill
- 运行长-lived services 使用 systemd and pm2
概念讲解
什么是进程?
A process is a running instance of a program. When you launch an application, the operating system creates a process, assigns it a unique PID (process ID), and gives it memory and resources. Processes can start child processes, forming a process tree. Understanding processes is essential for DevOps because servers run many processes simultaneously, and you need to monitor, restart, and sometimes kill them.
Listing and Inspecting Processes
The ps command lists processes. ps aux shows all processes on the system with details. 顶部 and htop provide a real-time, continuously updating view of CPU and memory usage per process. These tools help you find resource hogs, identify runaway processes, and understand what is running on a server at any given moment.
Signals and kill
Every process can receive signals — simple messages that tell it to do something. kill PID sends SIGTERM (terminate), which lets the process clean up before exiting. kill -9 PID sends SIGKILL, which forces immediate termination without cleanup. kill -l lists all available signals. Always try SIGTERM first; use SIGKILL only as a last resort because it can leave files corrupted or resources leaked.
Service Managers
For long-lived services like web servers and databases, you use a service manager. On Linux, systemd is the standard — systemctl 开始 nginx, systemctl 启用 nginx, systemctl 状态 nginx. For Node.js applications, pm2 is popular — it restarts crashed processes, manages logs, and handles clustering. Service managers ensure your applications start on boot and recover from failures automatically.
Process Lifecycle
Processes go through a lifecycle: created, running, sleeping, stopped, or zombie. A zombie process has finished but its parent hasn't acknowledged its exit — it still occupies a PID. Orphaned zombie processes can accumulate and exhaust the PID table. Use ps aux to spot them (they appear with status Z). Understanding this lifecycle helps you diagnose stuck services, memory leaks, and resource exhaustion — all common production issues that DevOps engineers investigate regularly.
示例代码
# List and inspect processes
ps aux | grep node # find all node processes
top -b -n 1 | head -20 # snapshot of top processes
# Start a process in the background
node server.js &
echo "Started with PID $!"
# Send signals to a process
kill -SIGTERM 12345 # gracefully stop process 12345
kill -SIGKILL 12345 # force kill if it won't stop
# Manage services with systemd
sudo systemctl start nginx
sudo systemctl enable nginx # start on boot
sudo systemctl status nginx
ps and 顶部 show 运行处理. kill 发送 signals 到停止它们. systemctl manages 长-lived services 通过 systemd, 标准 Linux init 系统.
动手试一试
- Cron Parser
Parse cron schedules for 处理 automation.
- TCP Port 引用
Look 上 TCP ports used 通过运行处理.
- Well-Known Port Lookup
查找哪个 service uses 特定 port.
常见错误
错误写法
Using kill -9 (SIGKILL) as 首先 option
正确做法
总是 try kill (SIGTERM) 首先. SIGKILL prevents 处理从 cleaning 上, 哪个能 corrupt files, leave ports 边界, or leak 数据库连接.
错误写法
Forgetting 到启用 services for boot
正确做法
Use systemctl 启用 <service> 所以 service 开始 automatically 之后 reboot. 不使用启用, 你的服务器将 是下 之后每个重启.
练习 处理 Management
1 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。