時間用久了我的阿里雲服務器下面的磁盤空間越來越少了,需要對無效的大文件進行清除刪除.我通過find 命令查找到nginx/logs/ 目錄西面有好幾個大的日志文件.因此想對其清除,但是當我刪除文件后,發現磁盤的空間並沒有變化.具體的操作如下:
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
32
33
34
35
36
|
//首先查看自己的系統目前的磁盤空間 可用2.1G
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 17G 2.1G 89% /
tmpfs 498M 0 498M 0% /dev/shm
//去查找nginx的access.log日志 日志文件大於100M的
[root@localhost ~]# find /usr/local/nginx/logs/ -size +100M -
exec
ls -lh {} \;
-rw-r--r-- 1 root root 1.2G Sep 7 10:23 /usr/local/nginx/logs/access.log
//刪除這個大文件 (請先備份或者遷移)
[root@localhost ~]# rm -rf /usr/local/nginx/logs/access.log
//查看磁盤空間~~OMG奇怪了.明明刪除了,可用空間還在是2.1G
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 17G 2.1G 89% /
tmpfs 498M 0 498M 0% /dev/shm
//查找nginx/logs/下 已經沒發現大文件了
[root@localhost ~]# find /usr/local/nginx/logs/ -size +100M -
exec
ls -lh {} \;
//重啟Nginx--為啥要重啟請看下方的原理
[root@localhost ~]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
//再次查看磁盤空間 瞬間萌萌噠.可用空間變成了3.3G了
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 16G 3.3G 83% /
tmpfs 498M 0 498M 0% /dev/shm
|
關於CentOS刪除文件后未釋放磁盤空間原因:在Linux或者Unix系統中,通過rm或者文件管理器刪除文件將會從文件系統的目錄結構上解除鏈接(unlink).然而如果文件是被打開的(有一個進程正在使用),那么進程將仍然可以讀取該文件,磁盤空間也一直被占用。而我刪除的是nginx的日志log文件,刪除的時候文件應該正在被使用.
lsof全名list opened files,也就是列舉系統中已經被打開的文件。我們都知道,linux環境中,任何事物都是文件,設備是文件,目錄是文件,甚至sockets也是文件。用好lsof命令,對日常的linux管理非常有幫助。
解決方法:首先獲得一個已經被刪除但是仍然被應用程序占用的文件列表,如下所示:
1
2
3
4
5
|
[root@localhost logs]# lsof |grep deleted
nginx 24747 root 12w REG 202,1 95721 663021 /usr/local/nginx/logs/access.log (deleted)
nginx 24749 root 12w REG 202,1 95721 663021 /usr/local/nginx/logs/access.log (deleted)
......
[root@localhost logs]#
|
如何讓進程釋放呢?一種方法是kill掉相應的進程,或者停掉使用這個文件的應用,讓os自動回收磁盤空間。