When we found that the disk is full, then delete some services log files, delete the discovery after disk space is occupied, but we use “du – sh / *” command, found no big file directory, then we should consider, delete the log file, just give all of the hard links to deleted, but there is a service or application, So we can’t see the actual data, but it still takes up disk space. In this case, you can restart the service or process that occupies log files to release the occupied disk space.

Simulated enterprise case:

1. Create a small simulated disk partition:

[root@oldboy ~]# dd if=/dev/zero of=/dev/oldboy bs=1k count=512512+0 write 524288 bytes (524 kB) copied, 0.00172664 SEC, 304 MB/ SEC [root@oldboy ~]# ll /dev/oldboy
-rw-r--r-- 1 root root 524288 10月 18 03:37 /dev/oldboy
Copy the code

2. Format the disk

[root@oldboy ~]# mkfs.ext4 /dev/oldboyMke2fs 1.41.12 (17-May-2010) /dev/oldboy is not a block special device. Do it anyway? (y,n) Y File system label = OS :Linux Block size =1024 (logBlock size =1024 (log=0) Stride=0 blocks, width=0 blocks 64 inodes, 512 blocks 25 blocks (4.88%) Reservedfor=1 Maximum filesystem blocks=524288 1 block group 8192 blocks per group, 8192 fragments per group 64 inodes per group Write superblocks and Filesystem accounting information: This filesystem will be automatically checked every 32 mounts or 180 days. You can use the virtualized filesystem to check every 32 days. whichever comes first. Use tune2fs -c or -i to override.Copy the code

3. Install an HTTPD service

[root@oldboy ~]# yum install HTTPD -- y
Mount virtual partition to /etc/ HTTPD /logs/
[root@oldboy ~]# mount -o loop /dev/oldboy /etc/httpd/logs/
[root@oldboy ~]# df -hFilesystem Size Used Avail Use% Mounted on /dev/sda3 8.8g 1.5g 7.0g 18% / TMPFS 244M 0 244M 0% /dev/shm /dev/sda1 190M 36M 145M 20% /boot /dev/sr0 3.7g 3.7g 0 100% / MNT /dev/oldboy 499K 15K 459K 4% /var/log/httpd
# enable HTTPD service logging, remove the comment 513 line.
[root@oldboy ~]# vi /etc/httpd/conf/httpd.conf* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 513#CustomLog logs/access_log common
# enable HTTPD service
[root@oldboy ~]# /etc/init.d/httpd startHTTPD: HTTPD: apr_sockaddr_info_get() failedfor oldboy
httpd: Could not reliably determine the serverFully qualified domain name, using 127.0.0.1 for ServerNameCopy the code

Enter the VIRTUAL machine IP address in the browser to access the mock Web service, trace HTTPD log files.

[root@oldboy ~]# tail -f /etc/httpd/logs/access_log
# refresh the page or write to the file directly, so that the partition for storing logs can be used up to 100%.
[root@oldboy ~]# for n in `seq 10000`; do echo "liangkai">>/etc/httpd/logs/access_log ; done
-bash: echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: write error: There is no space on the device.echo: Write error: There is no space on the deviceCopy the code

4. The disk space is used up. After the log file is deleted, the disk space is still used up.

[root@oldboy ~]# df -hFilesystem Size Used Avail Use% Mounted on /dev/sda3 8.8g 1.5g 7.0g 18% / TMPFS 244M 0 244M 0% /dev/shm /dev/sda1 190M 36M 145M 20% /boot /dev/sr0 3.7g 3.7g 0 100% / MNT /dev/oldboy 499K 489K 0 100% /var/log/httpd
[root@oldboy ~]# \rm -f /etc/httpd/logs/access_log
[root@oldboy ~]# df -hFilesystem Size Used Avail Use% Mounted on /dev/sda3 8.8g 1.5g 7.0g 18% / TMPFS 244M 0 244M 0% /dev/shm /dev/sda1 190M 36M 145M 20% /boot /dev/sr0 3.7g 3.7g 0 100% / MNT /dev/oldboy 499K 489K 0 100% /var/log/httpd
Copy the code

5. Run the lsof command to check the deleted files that are being used by the process

[root@oldboy ~]# lsof |grep deletedHTTPD 2008 root 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2008 root 8w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2013 apache 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2013 apache 8w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2014 apache 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2014 apache 8w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2015 apache 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2015 apache 8w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2016 apache 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2016 apache 8w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2017 apache 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2017 apache 8w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2018 apache 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2018 apache 8w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2019 apache 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2019 apache 8w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2020 apache 7w REG 7,0 420862 13 /var/log/ HTTPD /access_log (deleted) HTTPD 2020 apache 8w REG 7,0 420862 13 /var/log/httpd/access_log (deleted)
Copy the code

6. Restart the HTTPD service. The disk space is restored.

[root@oldboy ~]# /etc/init.d/httpd restartStop HTTPD: [confirm] Starting HTTPD: HTTPD: apr_sockaddr_info_get() failedfor oldboy
httpd: Could not reliably determine the server's fully qualified domain name, Using 127.0.0.1 for ServerName [confirm] [root@oldboy ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 8.8g 1.5g 7.1g 18% / TMPFS 244M 0 244M 0% /dev/shm /dev/sda1 190M 36M 145M 20% /boot /dev/sr0 3.7g 3.7g 0 100% / MNT /dev/oldboy 499K 78K 396K 17% /var/log/httpdCopy the code