消失的磁盘空间去哪了?

消失的磁盘空间去哪了?

过了几天,磁盘空间终究还是又满了?使用了之前的清理日志的方法只能腾出几百M的空间了。

使用 du 命令查看根目录,发现根目录只占17G,整个磁盘40G,剩下的20多G去哪了呢?

1
2
3
root@lolimay:/# du -sch $(ls --color=never --all) 2> /dev/null | sort -rh
17G total
17G .

通过这篇文章发现可以通过 lsof | grep deleted 命令找到被删除的文件(在文件系统中找不到了),但是由于程序还在运行,导致这个被删除的文件仍被占用,从而占用的磁盘空间不能被释放:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root@lolimay:/# lsof | grep deleted | awk '{for(i=1;i<=6;i++){printf "%s ", $i}; print $7/1048576 "MB" " "$8" "$9 }'
nginx 1605 root 2w REG 253,1 0.00390625MB 1454073 /www/wwwlogs/nginx_error.log
nginx 1605 root 4w REG 253,1 0.00390625MB 1454073 /www/wwwlogs/nginx_error.log
nginx 1605 root 5w REG 253,1 0.000277519MB 1861658 /www/server/nginx/off
nginx 1605 root 7w REG 253,1 0.0984421MB 1454074 /www/wwwlogs/api.lolimay.cn.log
nginx 1605 root 8w REG 253,1 0.000199318MB 1454082 /www/wwwlogs/api.lolimay.cn.error.log
nginx 1605 root 9w REG 253,1 0MB 1454083 /www/wwwlogs/csec.lolimay.cn.log
nginx 1605 root 10w REG 253,1 0MB 1454084 /www/wwwlogs/csec.lolimay.cn.error.log
nginx 1605 root 11w REG 253,1 0.00923347MB 1454085 /www/wwwlogs/devtool.lolimay.cn.log
nginx 1605 root 12w REG 253,1 0.0195312MB 1454086 /www/wwwlogs/devtool.lolimay.cn.error.log
nginx 1605 root 13w REG 253,1 0.00899124MB 1454154 /www/wwwlogs/kugou-dev.lolimay.cn.log
nginx 1605 root 14w REG 253,1 0.0195312MB 1454155 /www/wwwlogs/kugou-dev.lolimay.cn.error.log
nginx 1605 root 15w REG 253,1 6.06264MB 1454156 /www/wwwlogs/kugou.lolimay.cn.log
nginx 1605 root 16w REG 253,1 15.3351MB 1454157 /www/wwwlogs/kugou.lolimay.cn.error.log
nginx 1605 root 17w REG 253,1 0.000652313MB 1454158 /www/wwwlogs/kugouv2.lolimay.cn.log
nginx 1605 root 18w REG 253,1 0MB 1454159 /www/wwwlogs/kugouv2.lolimay.cn.error.log
nginx 1605 root 19w REG 253,1 15.8019MB 1454160 /www/wwwlogs/lolimay.cn.log
nginx 1605 root 20w REG 253,1 0.128906MB 1457104 /www/wwwlogs/mmic.lolimay.cn.log
nginx 1605 root 21w REG 253,1 0MB 1457105 /www/wwwlogs/mmic.lolimay.cn.error.log
nginx 1605 root 22w REG 253,1 20948.7MB 1457106 /www/wwwlogs/proxy.lolimay.cn.log <<< 好家伙,20G的空间竟然在这!
nginx 1605 root 23w REG 253,1 147.82MB 1457107 /www/wwwlogs/proxy.lolimay.cn.error.log
nginx 1606 www 2w REG 253,1 0.00390625MB 1454073 /www/wwwlogs/nginx_error.log
nginx 1606 www 4w REG 253,1 0.00390625MB 1454073 /www/wwwlogs/nginx_error.log
nginx 1606 www 5w REG 253,1 0.000277519MB 1861658 /www/server/nginx/off
nginx 1606 www 7w REG 253,1 0.0984421MB 1454074 /www/wwwlogs/api.lolimay.cn.log
nginx 1606 www 8w REG 253,1 0.000199318MB 1454082 /www/wwwlogs/api.lolimay.cn.error.log
nginx 1606 www 9w REG 253,1 0MB 1454083 /www/wwwlogs/csec.lolimay.cn.log
nginx 1606 www 10w REG 253,1 0MB 1454084 /www/wwwlogs/csec.lolimay.cn.error.log
nginx 1606 www 11w REG 253,1 0.00923347MB 1454085 /www/wwwlogs/devtool.lolimay.cn.log
nginx 1606 www 12w REG 253,1 0.0195312MB 1454086 /www/wwwlogs/devtool.lolimay.cn.error.log
nginx 1606 www 13w REG 253,1 0.00899124MB 1454154 /www/wwwlogs/kugou-dev.lolimay.cn.log

我们重启 nginx 进程即可释放这些空间:

1
service nginx restart

再看一下一下磁盘占用:

1
2
3
4
5
6
7
8
root@lolimay:/# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 1.8M 3.9G 1% /dev/shm
tmpfs 3.9G 532K 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/vda1 40G 17G 21G 45% /
tmpfs 783M 0 783M 0% /run/user/0

舒服了,瞬间有 21G 可用空间了。

为了解除后顾之忧,我们直接在 nginx 的配置文件中把这个站点的日志功能关闭:

1
2
access_log  off;
error_log off;