After Linux deletes a file, the space is not released.

Source: Internet
Author: User

After a file is deleted in Linux, the space is not released. ########################## restart the file application. In this way, the application can be released. Reason: linux only deletes the link between the file and the system directory structure while deleting the file. If a program is reading this file, the file still exists. If the file is not deleted, it is in the read status and still occupies disk space. Only when the program does not read this file, in order to completely release the space occupied by the file, only the process that uses the service or kills the service can completely delete the file and release the disk space. ######### Lsof file handle #################### 1. check the files used by a process: lsof-p pidlsof-p 24675 2. view the process that occupies a file or directory. And displays the process information to open the directory or file. Lsof director/filenamelsof/data/logs/jboss/cbsysApp01a/server. log ########## linux does not release space after deleting files ################# a server is found today the home Space is full, so I want to clear useless files. When I delete the files, I find that the available space has not changed OS: centos4.7: I found the current disk space usage: [root @ ticketb ~] # Df-hFilesystem Size Used Avail Use % Mounted on/dev/sda1 981 M 203 M 729 M 22%/none 16G 0 16G 0%/dev/shm/dev/sda9 2.9G 37 M 2.7G 2%/tmp/dev/sda7 4.9G 1.9G 2.7G 42%/usr/dev/sda8 2.9G 145 M 2.6G 6%/var/dev/mapper/vghome- lvhome 20G 19G 11 M 100%/home/dev/mapper/vgoradata-lvoradata 144G 48G 90G 35%/u01/oradata/dev/mapper/vgbackup-lvbackup 193G 7.8G 175G 5%/u01/backup use the following command to find useless files, then delete the [root @ ticke Tb ~] # Find/home/oracle/admin/dbticb/udump/-name "dbticb _*. trc "-mtime + 50 | xargs rm-rf. Then, check the disk space usage and find that no/home space has changed. [root @ ticketb ~] # Df-hFilesystem Size Used Avail Use % Mounted on/dev/sda1 981 M 203 M 729 M 22%/none 16G 0 16G 0%/dev/shm/dev/sda9 2.9G 37 M 2.7G 2%/tmp/dev/sda7 4.9G 1.9G 2.7G 42%/usr/dev/sda8 2.9G 145 M 2.6G 6%/var/dev/mapper/vghome- lvhome 20G 19G 11 M 100%/home/dev/mapper/vgoradata-lvoradata 144G 48G 90G 35%/u01/oradata/dev/mapper/vgbackup-lvbackup 193G 7.8G 175G 5%/u01/backup is depressing, why is the space not released when the file is deleted? The rm command should Delete it directly. Check the space occupied by/home [root @ ticketb ~] # Du-h -- max-depth = 1/home16K/home/lost + found2.6G/home/oracle2.6G/home. The space has been released, reason for not releasing disk space: in Linux or Unix systems, deleting a file through rm or file manager will remove the link from the directory structure of the file system (unlink ). however, if the file is opened (one process is in use), the process can still read the file, and the disk space is still occupied. When I delete the oracle Alert log File, the file should be used to obtain a list of files that have been deleted but are still occupied by the application, as shown below: [root @ ticketb ~] # Lsof | grep deletedoracle 12639 oracle 5 w REG 253,0 648 215907/home/oracle/admin/dbticb/udump/dbticb_ora_12637.trc (deleted) oracle 12639 oracle 6 w REG 253,0 16749822091 215748/home/oracle/admin/dbticb/bdump/alert_dbticb.log (deleted) oracle 12639 oracle 7u REG 36282, 0 0 12639/home/oracle/product/10.2.0/db_1/dbs/lkinstdbticb (deleted) oracle 16749822091 oracle 8 w REG 253,0 215748/home/ Oracle/admin/dbticb/bdump/alert_dbticb.log (deleted) oracle 12641 oracle 5 w REG 253,0 648 215907/home/oracle/admin/dbticb/udump/dbticb_ora_12637.trc (deleted) oracle 12641 oracle 6 w REG 253,0 16749822091 215748/home/oracle/admin/dbticb/bdump/alert_dbticb.log (deleted )... Oracle 23492 oracle 6 w REG 253,0 16749822091 215748/home/oracle/admin/dbticb/bdump/alert_dbticb.log (deleted) oracle 23492 oracle 7u REG 36282, 0 0/home/oracle/product/10.2.0/db_1/dbs/lkinstdbticb (deleted) oracle 23492 oracle 8 w REG 16749822091, 0 215748/home/oracle/admin/dbticb/bdump/alert_dbticb.log (deleted) oracle 23494 oracle 10u REG 36307, 0 0 10.2/home/oracle/product/. 0/db_1/dbs/lkinstrmandb (deleted) from the output result, we can see that/home/oracle/admin/dbticb/bdump/alert_dbticb.log is still in use. How can the process be released if the space is not released? One way is to kill the corresponding process, or stop the application that uses this file, so that the OS can automatically reclaim disk space. In this environment, many processes are using this file, stopping a process is a little troublesome, and there is a high risk that when a file is opened in linux, the Linux kernel creates a directory named "pid" for each process in/proc/"/proc/nnnn/fd/directory (nnnn is pid)" to save the process information, the sub-directory fd stores the fd (fd: file descriptor) of all files opened by the process ). By truncating files in the proc file system, the kill process forcibly requires the system to recycle the files allocated to being used. This is an advanced technology that is only used when the Administrator determines that it will not affect running processes. The application does not support this method well. When a file in use is truncated, it may cause unpredictable problems. So I still stop the application to solve the restart oracle database, we found that the space corresponding to/home/oracle/admin/dbticb/bdump/alert_dbticb.log was released and the disk space usage was viewed. We found that the space has been recycled [root @ ticketb ~]. # Df-hFilesystem Size Used Avail Use % Mounted on/dev/sda1 981 M 203 M 729 M 22%/none 16G 0 16G 0%/dev/shm/dev/sda9 2.9G 37 M 2.7G 2%/tmp/dev/sda7 4.9G 1.9G 2.7G 42%/usr/dev/sda8 2.9G 145 M 2.6G 6%/var/dev/mapper/vghome- lvhome 20G 2.6G 16G 15%/home/dev/mapper/vgoradata-lvoradata 144G 48G 90G 35%/u01/oradata/dev/mapper/vgbackup-lvbackup 193G 7.8G 175G 5%/u01/backup OK, solve the problem and finish the work ------------ The lsof command lsof full name list opened files, that is, list files already opened in the system. We all know that in linux, everything is a file, a device is a file, a directory is a file, and even sockets is a file. Therefore, using the lsof command is very helpful for daily linux management. Lsof is one of the most common commands in linux. The common output format is: Reference command pid user fd type device size node name. Common fields include: more visible manual. 1. The default COMMAND name is 9 characters long. You can use the + c parameter to specify the display width. If the parameter followed by + c is zero, the full name of the command is displayed. 2. PID: process ID 3. PPID parent process IP address, it is not displayed by default. It can be opened when the-R parameter is used. 4. ID of the PGID process group, which is not displayed by default. It can be opened when the-g parameter is used. 5. The UID for executing the USER command or the name of the USER logged in to the system. The user name is displayed by default. When the-l parameter is used, the UID is displayed. 6. FD is the File Descriptor number of the File, or the following content: (it is difficult to translate the corresponding meaning here. Keep English) Reference cwd current working directory; Lnn library references (AIX ); jld jail directory (FreeBSD); ltx shared library text (code and data); Mxx hex memory-mapped type number xx. m86 DOS Merge mapped file; mem memory-mapped file; mmap memory-mapped device; pd parent directory; rtd root directory; tr kernel trace file (OpenBSD ); txt program text (code and data ); V86 VP/ix mapped file; the File Descriptor number Display Mode of the file is: Reference r for read access; w for write access; u for read and write access; N for a Solaris NFS lock of unknown type; r for read lock on part of the file; R for a read lock on the entire file; w for a write lock on part of the file; W for a write lock on the entire file; u for a read and write lock of any length; U for a lock of unknown type; x for an SCO O PenServer Xenix lock on part of the file; X for an SCO OpenServer Xenix lock on the entire file; space if there is no lock. 7. TYPE refers to an IPv4 packet. IPv6 uses an IPv6 package, even if the address is IPv4, it is displayed as IPv6 and mapped to the IPv6 address; for more information about the LINK file in the DIR directory, see manual. 8. The SIZE of the DEVICE number 9 and SIZE file, represented by character special and block special, is left blank if it cannot be expressed in SIZE. Use-s parameter control. 10. The NODE Code or protocol of the local node file, such as TCP 11, NAME mount point, and full path of the file (the link will be resolved to the actual path ), or common examples of the addresses, ports, and statuses of both parties: 1. display the process of enabling the file/home/oracle/10.2.0/db_1/bin/tnslsnr [root @ svr-db-test ~] # Lsof/home/oracle/10.2.0/db_1/bin/tnslsnrCOMMAND pid user fd type device size node NAMEtnslsnr 3520 oracle txt REG 253,5 431062 11408866/home/oracle/10.2.0/db_1/bin/ tnslsnr 2. what program is running on port 22? [root @ svr-db-test ~] # Lsof-I: 22 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEsshd 3101 root 3u IPv6 8670 TCP *: ssh (LISTEN) sshd 4545 root 3u IPv6 4237972 TCP 203.aibo.com: ssh-> win-avbmq9e8ka7.gdgg.local: nsjtp-ctrl (ESTABLISHED) 3. display the files opened by the init process [root @ svr-db-test ~] # Lsof-c initCOMMAND pid user fd type device size node NAMEinit 1 root cwd DIR 253,0 4096 2/init 1 root rtd DIR 253,0 4096 2/init 1 root txt REG 253,0 43496 524446/sbin /initinit 1 root mem REG 253,0 130448 917826/lib64/ld-2.5.soinit 1 root mem REG 253,0 1678480 917827/lib64/libc-2.5.soinit 1 root mem REG 253,0 23520 917686/lib64/libdl-2.5.soinit 1 root mem REG 253,0 247528 917844/lib64/libs Epol. so.1init 1 root mem REG 253,0 95480 917845/lib64/libselinux. so.1init 1 root 10u FIFO 2311/dev/initctl 4. check the files opened by the process with process number 1 [root @ svr-db-test ~] # Lsof-p 1 command pid user fd type device size node NAMEinit 1 root cwd DIR 253,0 4096 2/init 1 root rtd DIR 253,0 4096 2/init 1 root txt REG 253,0 43496 524446/ sbin/initinit 1 root mem REG 253,0 130448 917826/lib64/ld-2.5.soinit 1 root mem REG 253,0 1678480 917827/lib64/libc-2.5.soinit 1 root mem REG 253,0 23520 917686/lib64/libdl-2.5.soinit 1 root mem REG 253,0 247528 917844/lib64/libsepo L. so.1init 1 root mem REG 253,0 95480 917845/lib64/libselinux. so.1init 1 root 10u FIFO 2311/dev/initctl 5. display the processes belonging to 3520 [root @ svr-db-test ~] # Lsof-g 3520 command pid pgid user fd type device size node NAMEtnslsnr 3520 3520 4096 oracle cwd DIR 253,5 11059201 3520/home/oracletnslsnr 3520 4096 oracle rtd DIR 253,0 3520 2/tnslsnr 3520 oracle txt REG 253,5 431062 11408866/home/oracle/10.2.0/db_1/bin/tnslsnrtnslsnr 3520 3520 130448 oracle mem REG 253,0 917826 3520/lib64/ld-2.5.sotnslsnr 3520 1678480 oracle mem REG 253,0 917827/lib64/ lib C-2.5.sotnslsnr 3520 3520 oracle mem REG 253,0 23520 917686/lib64/libdl-2.5.sotnslsnr 3520 3520 oracle mem REG 253,0 615136 917834/lib64/libm-2.5.sotnslsnr 3520 3520 oracle mem REG 253,0 141208 917829/lib64/libpthread-2.5.sotnslsnr 3520 oracle mem REG 253,0 109824/lib64/libnsl-2.5.sotnslsnr 917839 3520 oracle mem REG 253,5 3520 20706622/home/oracle/10.2.0/db_1/lib/libclntsh. so.1 0.1 tnslsnr 3520 3520 oracle mem REG 253,5 3803097 11410641/home/oracle/10.2.0/db_1/lib/libnnz10.sotnslsnr 3520 3520 83493 oracle mem REG 253,5 11407251/home/oracle/10.2.0/db_1/lib /libons. sotnslsnr 3520 3520 oracle mem REG 253,0 53880 917532/lib64/libnss_files-2.5.sotnslsnr 3520 3520 oracle mem REG 253,5 8545 11407615/home/oracle/10.2.0/db_1/lib/libskgxn2.sotnslsnr 3520 3520 oracle mem REG 253, 5 513705 11410332/home/oracle/10.2.0/db_1/lib/libocrutl10.sotnslsnr 3520 3520 636161 oracle mem REG 253,5 11410330 3520/home/oracle/10.2.0/db_1/lib/libocr10.sotnslsnr 3520 oracle mem REG 253,5 657825 11410331/home/oracle/10.2.0/db_1/lib/libocrb10.sotnslsnr 3520 3520 1745769 oracle mem REG 253,5 11410365 3520/home/oracle/10.2.0/db_1/lib/libhasgen10.sotnslsnr 3520 oracle mem REG 253,5 61985 1141 0366/home/oracle/10.2.0/db_1/lib/libclsra10.sotnslsnr 3520 3520 oracle 0u CHR 2553 3520/dev/nulltnslsnr 3520 2553 oracle 1u CHR 3520 3520/dev/nulltnslsnr oracle 2u CHR 2553/dev/nulltnslsnr 3520 3520 oracle 3 w REG 253,5 318853012 11633459/home/oracle/10.2.0/db_1/network/log/listener. logtnslsnr 3520 3520 oracle 4r FIFO 15661 3520 pipetnslsnr 3520 11776 oracle 5r REG 114105 79/home/oracle/10.2.0/db_1/network/mesg/nlus. msbtnslsnr 3520 3520 oracle 6r REG 253,5 46592 11407160/home/oracle/10.2.0/db_1/network/mesg/tnsus. msbtnslsnr 3520 3520 oracle 7 w FIFO 15662 3520 pipetnslsnr 3520 15665 oracle 8u IPv4 3520 TCP 203.aibo.com: ncube-lm (LISTEN) tnslsnr 3520 15666 oracle 9u unix 0xffff81021b7d6980/var/tmp /. oracle/s #3520.1 tnslsnr 3520 3520 oracle 10u unix 0xffff81021b7d 66c0 15668/var/tmp /. oracle/s #3520.2 6. search by Folder/home/oracle, but does not open sub-directories, used to display files opened by processes in the directory [root @ svr-db-test ~] # Lsof + d/home/oracleCOMMAND pid user fd type device size node NAMEtnslsnr 3520 oracle cwd DIR 253,5 4096 11059201/home/oracle 7. open the/home/oracle folder and Its subdirectory search to display the files opened by processes in the directory [root @ svr-db-test ~] # Lsof + D/home/oracle shows too much content, not 8. the lsof-I syntax is used to display qualified processes: lsof-I [46] [protocol] [@ hostname | hostaddr] [: service | port] 46 --> IPv4 or IPv6protocol --> TCP or UDPhostname --> Internet host namehostaddr --> IPv4 location service --> service name in/etc/service (either one or more) port --> port number (more than one) Example: [root @ svr-db-test ~] # Lsof-I tcp@192.168.2.245: 1521-nCOMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEoracle 15633 oracle 16u IPv4 4069605 TCP 192.168.2.203: 31580-> 192.168.2.245: ncube-lm (ESTABLISHED) or [root @ svr-db-test ~] # Lsof-I tcp@192.168.2.245: 1521 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAMEoracle 15633 oracle 16u IPv4 4069605 TCP 203.aibo.com: 31580-> 192.168.2.245: ncube-lm (ESTABLISHED) lsof-n does not convert the IP address to the hostname. The-n parameter 9 is not added by default. display a user's opened files (or files opened by the user's execution program) [root @ svr-db-test ~] # Lsof-u oracle or [root @ svr-db-test ~] # Lsof-u 0 10. Print only the process to facilitate shell scripts to call [root @ svr-db-test ~] # Lsof-tc sshd31014545 Note: Process Debugging commands: The truss, strace, and ltrace processes cannot be started, and the software runs slowly, the program's "SegmentFault" and so on are all headaches for every Unix system user, these problems can be quickly diagnosed by using truss, strace and ltrace, which are commonly used debugging tools ". ----- End -----

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.