How does Linux under the TMP directory file be deleted? __linux

Source: Internet
Author: User
when the file is deleted in the TMP directory.

Writing this article is when I'm going to reboot a long running Hadoop cluster, and I can't shut down the service. Look at the shell script see, to find the service of the PID file can be restarted, in general, the PID file will be stored in the TMP directory, but the PID file is gone to ensure that the TMP directory is not full, the system by default, daily processing of the TMP directory files, The principle is to use the Tmpwatch. tmpwatch function

Tmpwatch function: Removes files which haven ' t been accessed for a period of
Deletes files that have not been accessed for some time in the specified directory. tmpwatch Command Installation

If not fully installed, the system generally will not have tmpwatch command, you can manually install;

Yum Install Tmpwatch
tmpwatch Command Parameters Grammar
Tmpwatch [-afqv][--test][extended time] [catalogue ...]
some tmpwatch parameters
-U,--atime deletes files based on access time, default.
-M,--mtime deletes files based on modified time.
-C,--ctime deletes files based on creation time, for directories, based on Mtime.
-M,--dirmtime deletes the directory based on the directory's modified time instead of the access time.
-A,--all deletes all file types, not just plain files, symbolic links and directories.
-D,--nodirs does not attempt to delete the directory, even if it is an empty directory.
-D,--nosymlinks does not attempt to delete the symbolic link.
-F,--force forced deletion.
-Q,--quiet only reports error messages. -
S,--fuser try to use the "fuser" command if the file is already open before deleting it. is not enabled by default. -
T,--test only for testing and does not really delete files or directories. -
u,--exclude-user=user does not delete who belongs to the file.
-V,--verbose print details. -
x,--exclude=path excludes the path, and if the path is a directory, all of the files it contains are excluded. If the path does not exist, it must be an absolute path that does not contain a symbolic link. -
x,--exclude-pattern=pattern excludes the path under a rule.

If Tmpwatch is already installed in the system, there is a Tmpwatch script file under the/etc/cron.daily/directory, and the script file in My computer reads as follows:

[Root@bogon conf]# more/etc/cron.daily/tmpwatch 
#!/bin/sh flags=-umc/usr/sbin/tmpwatch
"$flags"-X/ tmp/. X11-unix-x/tmp/. Xim-unix \
    -x/tmp/.font-unix-x/tmp/. Ice-unix-x/tmp/. Test-unix \
    x '/tmp/hsperfdata_* ' 10d/tmp
/usr/sbin/tmpwatch ' $flags ' 30d/var/tmp for
D in/var/{cache/ Man,catman}/{cat?,x11r6/cat?,local/cat?}; Do
    if [d "$d"] then
    /usr/sbin/tmpwatch "$flags"-F 30d "$d"
    fi
Done

As you can see from the code, files that have not been accessed for 10 days under the TMP directory are deleted, and files that have not been accessed for some time in the specified directory are searched recursively from the specified directory.
Note: The script file above is a daily scheduled task, and if you want to delete files that have not been accessed or modified within two hours, you cannot modify them in this script because the execution cycle for this script is 1 days. You can use the following command directly to "Delete xxx days have not been accessed/modified files"

Tmpwatch 20/tmp/

If you do not write units, the default is the hour, above is deleted 20 hours has not been accessed/modified files. Tmpwatch to bring us problems.

Many of the services in the system of PID files will be stored in the/tmp directory, from the above script file can see the system will periodically delete the files are not accessed, which will cause many services can not be shut down properly. Solving Method

You can use the-u parameter to exclude

-U,--exclude-user=user
              Don ' t remove files owned by user, which can is an user name or numeric user ID.
Please use the man tmpwatch to view the specific parameters.

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.