내 Linux 서버에 일부 오래된 파일이 있는데 임계값을 기준으로 5일보다 오래된 파일을 삭제해야 합니다.
log_space_checker() {
Use=$(df -kh /logs | awk 'END{gsub("%",""); print $4}');
DATAUSE=$(df -kh /logs | awk 'END{gsub("%",""); print $4}');
}
remove_files(){
RemoveFiles="/logs/abc/abc.log.*"
find "$RemoveFiles" -mtime +1 -type f -delete
}
disk_space_monitor() {
log_space_checker;
if [[ $DATAUSE -gt $TH ]] ; then
remove_files;
fi
}
TH=7
disk_space_monitor
스크립트가 맞나요?
답변1
나, 난 이런 짓을 할 거야
#!/bin/bash
#
########################################################################
#
log_space_checker() {
local target="$1" # Log file template
local directory="${target%/*}" # Directory holding log files
df -k "$directory" | awk 'NR>1 {print gensub("^.* ([1-9][0-9]*)%.*", "\\1", 1)}'
}
########################################################################
#
remove_files(){
local logfiles="$1" # Log file template
find $logfiles -mtime +1 -type f -print ## -delete
}
########################################################################
#
disk_space_monitor() {
local threshold="$1" # Threshold%
local target="$2" # Log files to delete
local pct_used=$(log_space_checker "$2");
if [[ $pct_used -gt $threshold ]]
then
remove_files "$2"
fi
}
########################################################################
# Go
#
threshold=7 # % usage above which we will delete
logfiles='/logs/abc/abc.log.*' # ...log files matching this pattern
disk_space_monitor "$threshold" "$logfiles"