Linux 系统资源管理
2.1 内存管理
bash
# 查看内存使用
free -h # 人性化显示(GB/MB)
free -m # 以 MB 为单位
cat /proc/meminfo # 详细内存信息
# 输出示例
# total used free shared buffers cached
# Mem: 7.7Gi 3.2Gi 4.5Gi 256Mi 200Mi 2.5Gi1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
bash
# 清理缓存(需root)
sync # 同步文件系统
echo 3 > /proc/sys/vm/drop_caches # 清理缓存
# 查看内存占用最高的进程
ps aux --sort=-%mem | head -6
top -o %MEM1
2
3
4
5
6
7
2
3
4
5
6
7
2.2 磁盘管理
bash
# 查看磁盘使用
df -h # 人性化显示
df -i # 查看 inode 使用
du -sh dir/ # 查看目录大小
du -h --max-depth=1 # 查看子目录大小
# 列出文件大小(降序)
ls -lhS
# 查看磁盘 IO
iostat -x 1 5 # 每秒刷新,共5次
iotop # 交互式查看 IO 占用1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
2.3 CPU 管理
bash
# 查看 CPU 信息
cat /proc/cpuinfo
lscpu # 简化信息
# 查看 CPU 使用率
top # 交互式
htop # 更友好(需安装)
mpstat 1 5 # 每秒刷新,共5次
# 查看 CPU 核心数
nproc # 逻辑核心数
lscpu | grep "^CPU(s):" # 物理核心数1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
2.4 进程管理
bash
# 查看进程
ps aux | grep nginx # 查找 nginx 进程
ps -ef | head -20 # 完整格式
# 进程状态
# R = Running, S = Sleeping, Z = Zombie, T = Stopped
ps aux --sort=-%cpu # 按CPU排序
ps aux --sort=-%mem # 按内存排序
# 实时进程监视
top
# 交互命令:
# P - 按CPU排序
# M - 按内存排序
# q - 退出
# k - 杀死进程
# r - 调整优先级1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2.5 进程控制
bash
# 杀死进程
kill PID # 正常终止
kill -9 PID # 强制终止
kill -15 PID # 优雅终止(默认)
killall nginx # 按名字杀
# 向进程发送信号
kill -l # 列出所有信号
kill -2 PID # SIGINT(Ctrl+C)
kill -19 PID # SIGSTOP(暂停)
kill -18 PID # SIGCONT(继续)
# 后台/前台
command & # 后台运行
nohup command & # 忽略挂断信号后台运行
nohup command > out.log 2>&1 &
# jobs -l # 查看后台任务
# fg %1 # 切到前台
# bg %1 # 切到后台
# 按进程名杀
pkill nginx
pgrep -f "nginx" # 查找进程PID1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2.6 systemd 服务管理
bash
# 常用命令
systemctl start nginx # 启动
systemctl stop nginx # 停止
systemctl restart nginx # 重启
systemctl reload nginx # 重载配置
systemctl status nginx # 查看状态
# 开机自启
systemctl enable nginx # 启用
systemctl disable nginx # 禁用
systemctl is-enabled nginx # 检查状态
# 查看服务日志
journalctl -u nginx # nginx 日志
journalctl -u nginx -f # 实时跟踪
journalctl --since "1 hour ago"
# 开机启动失败排查
systemctl --failed
systemctl list-dependencies nginx1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2.7 日志管理
bash
# 系统日志位置
/var/log/syslog # Ubuntu 系统日志
/var/log/messages # CentOS 系统日志
/var/log/secure # 安全日志
/var/log/nginx/access.log # Nginx 访问日志
/var/log/nginx/error.log # Nginx 错误日志
# 查看日志
tail -f /var/log/syslog # 实时跟踪
tail -n 100 /var/log/xxx # 最后100行
# 日志轮转(logrotate)
cat /etc/logrotate.conf
cat /etc/logrotate.d/nginx1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
2.8 定时任务
bash
# 查看 crontab
crontab -l # 当前用户
crontab -e # 编辑
# 格式:分 时 日 月 周 命令
# * * * * * command
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ └─── 星期 (0-7, 0和7是周日)
# │ │ │ └────── 月 (1-12)
# │ │ └──────── 日 (1-31)
# │ └────────── 时 (0-23)
# └──────────── 分 (0-59)
# 示例
0 3 * * * /root/backup.sh # 每天凌晨3点
0 */2 * * * /path/to/task.sh # 每2小时
0 9-18 * * 1-5 /path/to/task.sh # 工作日9点到18点每整点
30 4 * * 0 /path/to/weekly.sh # 每周日凌晨4:30
# 查看定时任务执行日志
grep CRON /var/log/syslog1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2.9 系统监控
bash
# 系统整体
uptime # 运行时间 + 负载
w # 在线用户 + 负载
hostname # 主机名
# 监控工具
vmstat 1 5 # 虚拟内存统计
iostat -x 1 3 # IO 统计
netstat -tulpn # 端口监听
ss -tulpn # 更快的 netstat
# 监控脚本示例
#!/bin/bash
while true; do
echo "=== $(date) ==="
echo "CPU: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}')%"
echo "MEM: $(free -h | awk '/^Mem:/ {print $3}')"
echo "DISK: $(df -h / | awk 'NR==2 {print $5}')"
sleep 3
done1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2.10 资源限制
bash
# 查看资源限制
ulimit -a
# 常用限制
ulimit -n 65535 # 文件描述符数量
ulimit -u 4096 # 用户最大进程数
# 永久修改(/etc/security/limits.conf)
# * soft nofile 65535
# * hard nofile 655351
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
[[返回 Linux 首页|../index]]