服务器内存满日常运维命令

bash 复制代码
# 查看哪些进程占用swap
for i in $(ls /proc|grep -E '^[0-9]+$');do awk '/Swap:/{s+=$2}END{print '"$i"',s/1024"MB"}' /proc/$i/smaps 2>/dev/null;done|sort -k2 -nr


dmesg -T | grep -i oom
# 持续监控内存变化
watch -n1 free -h


# 看top两个swap进程是什么
ps -ef |grep -E '2979|2973'

# 看进程内存完整状态(RSS/VMSwap)
cat /proc/2979/status |grep -E 'VmRSS|VmSwap'
cat /proc/2973/status |grep -E 'VmRSS|VmSwap'

# 看系统整体swap概况
free -h
swapon --show

# 看系统内存回收、swapin/out事件
vmstat 1

# 看OOM与kswapd内核日志
dmesg -T |grep -E 'kswapd|OOM'



#!/bin/bash
for pid in /proc/[0-9]*; do
    pid=${pid##*/}
    awk '/Swap:/{s+=$2}END{print '"$pid"', s/1024}' "/proc/$pid/smaps" 2>/dev/null
done | sort -k2 -nr > swap_pid.txt




> 境:Docker,宿主机看到 PID,想定位是哪个容器。

## 方法 1:lsns 看 pid 所属 net namespace(最简单)

```
lsns -t net -p <宿主机PID>
```

拿到`NS`编号,再匹配容器的 net ns。
或者直接看进程的 namespace 链接:

```
readlink /proc/<PID>/ns/net
# 输出类似 net:[4026532421]
```

然后遍历容器找对应 net inode:

```
for c in $(docker ps -q); do
  echo $c $(docker inspect --format '{{.NetworkSettings.SandboxKey}}' $c)
done
```

SandboxKey 里面路径末尾的数字就是 net ns inode,对上就是对应容器。

## 方法 2:docker top(已知容器,看内部进程)

```
docker top <容器ID/名称>
```

> 
> 注意:`docker top`显示的是**宿主机 PID**。

## 方法 3:通过 /proc/<pid>/cgroup 定位(最常用)

宿主机拿到进程 PID:

```
cat /proc/<PID>/cgroup
```

输出会有类似:

```
12:cpuset:/docker/xxxxxxx
11:memory:/docker/xxxxxxx
```

`/docker/`后面那串字符串就是**容器完整 ID**,直接:

```
docker inspect xxxxxxx
```

> 
> 如果是 k8s containerd,cgroup 路径会是 `/kubepods/.../xxx`,那是 pod ID。

### 一键脚本:输入宿主机 PID,直接输出容器名

```
PID=12345
cid=$(cat /proc/$PID/cgroup | grep -oP 'docker/\K[0-9a-f]+' | head -1)
if [ -n "$cid" ];then
    docker ps --filter "id=${cid}"
else
    echo "该进程不属于docker容器"
fi











#!/bin/bash

pid_list=(
34441
1052
48060
29241
1044
29442
29231
1062
19297
1054
417
31777
1058
29443
52698
65943
70783
28803
42936
43035
54594
43036
42998
24882
28759
18159
28771
28820
28819
2975
69839
29239
50024
28767
26841
42995
26819
26845
9272
25989
26846
2360
57012
57020
1099
54994
57137
65122
42517
45762
49176
43514
1160
68148
70689
65061
1317
27738
65845
1449
25059
60694
56463
8754
20724
65123
65396
5191
4280
3302
1347
22809
5199
48971
47945
5225
2976
52753
67991
71106
57161
71114
71183
31449
57141
70573
57146
57145
7106
39412
65192
58401
31418
2974
34557
30045
1320
8803
31590
6921
22698
18067
22706
18015
3325
19281
53473
66696
10377
57735
16320
27017
11343
18068
18075
1958
34917
22718
22721
2053
61323
49466
61598
61977
)

printf "%-8s %-16s %-14s %s\n" PID RSS 归属 命令行
printf "================================================================================\n"

for pid in "${pid_list[@]}"; do
    if [[ ! -d /proc/${pid} ]]; then
        printf "%-8s %-16s %-14s %s\n" "${pid}" "gone" "[DEAD]" "进程已消失"
        continue
    fi
    rss_kb=$(awk '/VmRSS/ {print $2}' /proc/${pid}/status)
    rss_mb=$(echo "scale=2; ${rss_kb}/1024" | bc)
    cgroup=$(head -n1 /proc/${pid}/cgroup)
    if [[ "${cgroup}" =~ docker || "${cgroup}" =~ kubepods ]]; then
        belong="容器进程"
    else
        belong="宿主机裸进程"
    fi
    cmd=$(cat /proc/${pid}/cmdline | tr '\0' ' ' | cut -c1-90)
    printf "%-8s %-16s %-14s %s\n" "${pid}" "${rss_mb}MB" "${belong}" "${cmd}"
done



debug = true
insecure-entitlements = ["network.host", "security.insecure"]

[registry."harbor.dev.ai-finance.cn"]
  http = true


# 删除旧builder
docker buildx rm armbuilder

# 确保toml没有windows换行
dos2unix /home/buildkitd.toml

# 创建builder,加载修正后的toml
docker buildx create --name armbuilder \
  --driver docker-container \
  --driver-opt network=host \
  --buildkitd-config /home/buildkitd.toml \
  --platform linux/amd64,linux/arm64

# bootstrap启动
docker buildx inspect armbuilder --bootstrap
docker buildx use armbuilder

tar czvf - -C arm2.5/ .|split -d -b 1G - cv.tar.gz

cat cv.tar.gz* | tar -zxvf - -C ./

相关推荐
honsor7 分钟前
以太网温湿度传感器|动环监控核心采集设备,协议、组网、Zabbix接入实战
linux·服务器·ubuntu
Android系统攻城狮8 分钟前
Linux FFmpeg深度解析之avcodec_default_get_buffer2调用流程与实战(二十)
linux·运维·服务器·ffmpeg·音视频进阶·ffmpeg音视频
深圳恒讯9 分钟前
香港云主机硬盘满了怎么办?Linux和Windows在线扩容操作教程
linux·运维·windows
饭后一颗花生米11 分钟前
GitHub Actions 自动化:5 个真实运维提效案例
运维·自动化·github
十碗阳春面12 分钟前
阿里云 Ubuntu 22.04 部署 Flarum 论坛:从空白服务器到中文界面、公网 IP 访问
服务器·ubuntu·阿里云
Android系统攻城狮17 分钟前
Linux FFmpeg深度解析之av_parser_init调用流程与实战(二十一)
linux·运维·服务器·ffmpeg·音视频进阶·ffmpeg音视频
HideF33 分钟前
Linux 查看用户和组
linux
Canmag 兴隆磁性38 分钟前
钕铁硼磁片取向方向检测自动化方案:从人工目检到流水线在线分选的工程实践
运维·数据库·自动化·工业自动化·钕铁硼·自动化检测·磁材料
a59865239 分钟前
ARM服务器搭建云手机靠谱吗?个人自建方案优缺点总结
服务器·arm开发·智能手机·云手机
寺中人1 小时前
CentOS 7进程与任务管理
linux·运维·centos·服务器搭建与管理·linux运维命令·nohup screen