source:http://blog.chinaunix.net/uid-26212859-id-3567875.html
經常會把臨時性的log或抓包等文件放在/tmp目錄下,重啟后發現文件都丟失了。查下資料發現,可以通過設置解決這個問題。
在/etc/default/目錄下有個rcS文件,文件內容如下:
#
# /etc/default/rcS
#
# Default settings for the scripts in /etc/rcS.d/
#
# For information about these variables see the rcS(5) manual page.
#
# This file belongs to the "initscripts" package.
TMPTIME=0
SULOGIN=no
DELAYLOGIN=no
UTC=yes
VERBOSE=no
FSCKFIX=no
RAMRUN=no
RAMLOCK=no
紅色字體TMPTIME=0。值為0表示重啟后刪除文件,值為-1就不會自動刪除文件,職位正整數表示/tmp目錄下文件保留時間。
關於linux tmp下文件自動刪除的問題
source:http://www.tuicool.com/articles/6Jj6rq
關於linux tmp下文件自動刪除的問題
近日發現有一台機器tmp 下放置的文件無辜丟失,而且排查發現是自動丟失,並且,只是刪除10天之前的文件….
本來以為是哪位寫了一個自動執行腳本, find 了一下10天前的文件刪除了….
結果,排查所有用戶的crontab 計划,沒有任何用戶執行了自動腳本
監測了一下服務器登錄情況,在刪除文件期間也沒有任何人登錄
最終,通過不懈的百度,終於找到正解
從/var/log/cron 日志中發現,服務器除了調用用戶的計划任務外,還會執行系統自己的,比如:
/etc/cron.hourly
/etc/cron.daily
進入 /etc/cron.daily
可以看到一個tmpwatch
#! /bin/shflags=-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_*' -X '/tmp/.hdb*lock' -X '/tmp/.sapstartsrv*.log' \
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
可以看到調用了一個叫tmpwatch 的腳本,並且,我們可以看到傳入參數中 對我們有意義的有 /tmp 240
然后我們 man tmpwatch
SYNOPSIS
tmpwatch [-u|-m|-c] [-MUadfqstvx] [--verbose] [--force] [--all]
[--nodirs] [--nosymlinks] [--test] [--fuser] [--quiet]
[--atime|--mtime|--ctime] [--dirmtime] [--exclude <path>]
[--exclude-user <user>] <hours> <dirs>
有一個 hours
240 = 10*24 整好是10天
具體原因是 tmp是一個特殊的文件夾,系統會自動清理,所以大家最好不要把文件放到這個地方,被清理了就不好了CentOS6以下系統(含)使用watchtmp + cron來實現定時清理臨時文件的效果,這點在CentOS7發生了變化,在CentOS7下,系統使用systemd管理易變與臨時文件,與之相關的系統服務有3個:
systemd-tmpfiles-setup.service :Create Volatile Files and Directories
systemd-tmpfiles-setup-dev.service:Create static device nodes in /dev systemd-tmpfiles-clean.service :Cleanup of Temporary Directories
相關的配置文件也有3個地方:
/etc/tmpfiles.d/*.conf
/run/tmpfiles.d/*.conf
/usr/lib/tmpfiles.d/*.conf
/tmp目錄的清理規則主要取決於/usr/lib/tmpfiles.d/tmp.conf文件的設定,默認的配置內容為:
# This file is part of systemd. # # systemd is free software; 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 version 2.1 of the License, or # (at your option) any later version. # See tmpfiles.d(5) for details # Clear tmp directories separately, to make them easier to override v /tmp 1777 root root 10d # 清理/tmp下10天前的目錄和文件 v /var/tmp 1777 root root 30d # 清理/var/tmp下30天前的目錄和文件 # Exclude namespace mountpoints created with PrivateTmp=yes x /tmp/systemd-private-%b-* X /tmp/systemd-private-%b-*/tmp x /var/tmp/systemd-private-%b-* X /var/tmp/systemd-private-%b-*/tmp
我們可以配置這個文件,比如你不想讓系統自動清理/tmp下以tomcat開頭的目錄,那么增加下面這條內容到配置文件中即可:
x /tmp/tomcat.*
