Cleanup rules for the/tmp directory under Linux

Source: Internet
Author: User

This article describes the cleanup rules for the Linux/tmp directory, and the RHEL6 and RHEL7 will be cleaned up in two completely different ways.

RHEL6

Tmpwatch command

Tmpwatch is a command specifically designed to address the need to "delete files that have not been accessed/modified by XXX days".

Installation:

Yum Install tmpwatch.x86_64

Use:

MansTmpwatchtmpwatch-Removes fileswhichHaven't been accessed for a period of time.by default, Tmpwatch dates files by their atime (Access Time), not their mtime (modification Time). the TimeParameter defines the threshold forremoving files. If thefileHave not been accessed for  Time, thefileis removed. the Timeargument is a number with an optional Single-character suffix specifying the units:m forMinutes, H forHours, D forDays . If no suffix is specified, TimeIsinchhours. -U,--Atime make the decision about deleting afileBased on thefile'sAtime (Access Time).          This is the default. Note that the periodic updatedbfilesystem scans keep the atime of directories recent. -M,--Mtime make the decision about deleting afileBased on thefile'sMtime (Modification Time) instead of the atime. -C,--CTime make the decision about deleting afileBased on thefile'sCTime (inode change Time) instead of the atime; forDirecto‐ries, MakeThe decision based on the mtime. -M,--Dirmtime make the decision about deleting a directory based on the directory's Mtime (modification time) instead of the atime;Completely Ignore Atime forDirectories.

Example: (Clear the/tmp directory for 30 days without access to the file)

[Email protected] ~]# Tmpwatch--atime 30d/tmp

RHEL7

Systemd-tmpfiles-clean.service Service

Service: systemd-tmpfiles-clean.service

When will the service be executed?

The execution of the service under Linux can be managed according to Systemd-tmpfiles-clean.timer

[Email protected] ~]#Cat/usr/lib/systemd/system/systemd-tmpfiles-clean.timer# Thisfileis part of systemd.## systemd is FreeSoftware; You can redistribute it and/or modify it# under the terms of the GNU Lesser general public License as published by# the free software Foundation ; Either version2.1of the License, or# (at your option) any later version. [Unit] Description=Daily Cleanup of temporary directoriesdocumentation=Mans: TMPFILES.D (5)Mans: Systemd-tmpfiles (8) [Timer]onbootsec=15minOnUnitActiveSec=1d# onbootsec indicates that the time-point relative to the machine is started # indicates a relative to the matching unit (the unit under this tab= specified cell) The last time the point was started

The above configuration file indicates that the service is executed in two cases

    1. Boot 15-minute execution service
    2. The service is executed 1 days after the last execution of the service

How does the service work?

[Email protected] ~]#Cat/usr/lib/systemd/system/systemd-tmpfiles-clean.service# Thisfileis part of systemd.## systemd is FreeSoftware; You can redistribute it and/or modify it# under the terms of the GNU Lesser general public License as published by# the free software Foundation ; Either version2.1of the License, or# (at your option) any later version. [Unit] Description=Cleanup of temporary directoriesdocumentation=Mans: TMPFILES.D (5)Mans: Systemd-tmpfiles (8) Defaultdependencies=noconflicts=Shutdown.targetafter=systemd-readahead-collect.service Systemd-readahead-replay.service Local-fs.target Time-Sync. Targetbefore=Shutdown.target[service]type=Oneshotexecstart=/usr/bin/systemd-tmpfiles--Cleanioschedulingclass=idle# Type=oneshot This option applies to service # command files that perform only one task and then immediately exit/usr/bin/systemd-tmpfiles# Command Parameters--clean# by regular execution/usr/bin/systemd-tmpfiles--clean Complete cleanup

Command: /usr/bin/systemd-tmpfiles

[Email protected] ~]#/usr/bin/systemd-tmpfiles--helpsystemd-tmpfiles [OPTIONS ...] [CONFIGURATION FILE ...]  Creates, deletes and cleans up volatile and temporary files and directories. -H--Help Show this Help--version Show package version--create Create marked files/Directories--Clean marked directories--remove Remove marked files/Directories--Boot Execute actions with safe at boot--prefix=PATH only apply rules with the specified prefix--exclude-prefix=PATH Ignore rules with the specified prefix--root=PATH Operate on an alternate filesystem root#--clean will clean up the tagged file directory

Which directories are flagged and what are the tags?

Defined in configuration file/usr/lib/tmpfiles.d/tmp.conf

Configuration file: /usr/lib/tmpfiles.d/tmp.conf

[Email protected] ~]#Cat/usr/lib/tmpfiles.d/tmp.conf# Thisfileis part of systemd.## systemd is FreeSoftware; You can redistribute it and/or modify it# under the terms of the GNU Lesser general public License as published by# the free software Foundation ; Either version2.1of the License, or# (at your option) any later version.# see TMPFILES.D (5) fordetails# Clear tmp directories separately, to Makethem easier to Overridev/tmp1777root root 10dv/var/tmp1777root root 30d# Exclude namespace mountpoints created with Privatetmp=Yesx/tmp/systemd-private-%b-*X/tmp/systemd-private-%b-*/tmpx/var/tmp/systemd-private-%b-*X/var/tmp/systemd-private-%b-*/tmp

X is based on"Lifespan"When a field cleans out an expired file, the specified path and all content under that path are ignored. Can be in"Path"use shell-style wildcard characters in the field. Note that this protective measure is not valid for R and R. X is based on"Lifespan"When a field cleans out an expired file, only the specified path itself is ignored and the other content under that path is not included. Can be in"Path"Use shell-style wildcard characters in the field. Note that this protective measure is not valid for R and R.

The above configuration indicates:

    1. Clear the/tmp directory for more than 10 days, but the contents of the matching/tmp/systemd-private-%b-* directory and its path will be preserved

    2. Cleans up content of/var/tmp directory for more than 30 days, but all content under the directory that matches/var/tmp/systemd-private-%b-* and its path is preserved

Summarize
    1. RHEL6 Use the Tmpwatch command to clean the/TMP directory based on conditions such as the access time of the file, which can be performed on a regular basis using Crond daemon

    2. RHEL7 the cleanup of temporary files according to the service Systemd-tmpfiles-clean.service, the cleanup rules are defined in the configuration file/usr/lib/tmpfiles.d/tmp.conf, and the Invoke command is/usr/bin/ Systemd-tmpfiles--clean, execution time relies on Systemd-tmpfiles-clean.timer for management

 

Cleanup rules for the/tmp directory under Linux

Related Article

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.