로그 파일 삭제 쉘 스크립트 만들기(날짜 20xx, 용량)

공정위 광고 문구 Ver 3.1
공정위 광고 문구 Ver 3.2(모바일 전용)

httpd.conf 파일에 access_log 및 error_log를 설정 했을 때 logrotate 설정이 아닌 경우 로그 파일 삭제 쉘 스크립트가 필요할 때가 있습니다.

✅ 로그 파일 삭제 쉘 스크립트

## access_log* 1G 이상 삭제

#!/bin/bash
find /usr/local/tomcat9/logs/  -name "access.*" -size +800M -exec rm -f {} \;

##  log_* 9일 이상

#!/bin/bash
find /usr/local/apache/logs/access/  -name "access_*" -mtime +9 -exec rm -f {} \;

# 다른 예시 
find /usr/local/apache/logs/error/ -name "error_log.*" -mtime +7 -exec rm -f {} \;
find /usr/local/apache/logs/ -name "access_log.*" -mtime +7 -exec rm -f {} \;

✅ crontab 설정하기

크론탭 설정을 진행 해 줍니다.

~]# crontab -e
# 매주 금요일 4시 30분 9일 이상 된 access_* 파일 삭제 
30 04 * * 5 shell/log_delete.sh

~]# systemctl restart crond ## crontab -e 로 수정한 경우 굳이 재시작을 할 필요 없이 적용됩니다.

~]# systemctl status  crond
● crond.service - Command Scheduler
     Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; preset: enabled)
     Active: active (running) since Sun 2024-01-21 14:19:33 UTC; 6s ago
   Main PID: 13735 (crond)
      Tasks: 1 (limit: 10823)
     Memory: 960.0K
        CPU: 2ms
     CGroup: /system.slice/crond.service
             └─13735 /usr/sbin/crond -n

Jan 21 14:19:33 ip-172-31-11-71.ap-northeast-2.compute.internal systemd[1]: Started Command Scheduler.
Jan 21 14:19:33 ip-172-31-11-71.ap-northeast-2.compute.internal crond[13735]: (CRON) STARTUP (1.5.7)
Jan 21 14:19:33 ip-172-31-11-71.ap-northeast-2.compute.internal crond[13735]: (CRON) INFO (Syslog will be used instead of sendmail.)
Jan 21 14:19:33 ip-172-31-11-71.ap-northeast-2.compute.internal crond[13735]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 8% if used.)
Jan 21 14:19:33 ip-172-31-11-71.ap-northeast-2.compute.internal crond[13735]: (CRON) INFO (running with inotify support)
Jan 21 14:19:33 ip-172-31-11-71.ap-northeast-2.compute.internal crond[13735]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
쉘 스크립트(Shell Script)

리눅스 스크립트는 리눅스 운영 체제에서 해석하는 스크립트 언어로 작성된 명령 또는 명령 집합입니다. 리눅스 시스템에서 반복 작업을 자동화 할 때 많이 사용하게 됩니다.

쉘 스크립트는 Bash Shell Script 외에도 Perl, Python, Ruby 등 다양한 스크립트 언어로 작성할 수 있습니다.

ℹ️ 제휴 안내
본 사이트의 콘텐츠에는 제휴 링크가 포함되어 있습니다. 방문자가 이 링크를 통해 상품 또는 서비스를 구매하면 본 사이트는 판매처로부터 수수료를 지급받습니다. 이 과정에서 구매자가 지불하는 금액(이벤트 할인 시 금액이 내려갑니다↓)은 오르지 않습니다. 게시된 가격·할인·재고 정보는 작성 시점 기준이며 실제와 다를 수 있으므로, 구매 전 판매처에서 최종 확인하시기 바랍니다. 상품 선정과 평가는 자체 기준에 따라 작성되며, 수수료 지급 여부가 소개 순서나 평가 내용에 영향을 주지 않습니다.

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

Prove your humanity: 1   +   5   =