-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20-delete-old-logs.sh
More file actions
54 lines (39 loc) · 1.21 KB
/
20-delete-old-logs.sh
File metadata and controls
54 lines (39 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
USERID=$(id -u)
R="\e[31M"
G="\e[32m"
Y="\e[33m"
B="\e[34m"
N="\e[0m"
LOG_FOLDER="/var/log/shellscript-logs" # inorder to create /var and rm dir need soudo access so check_root function
SCRIPT_NAME=$(echo $0 | cut -d "." -f1)
LOG_FILE="$LOG_FOLDER/$SCRIPT_NAME.log"
# first whant to learn about souce dir where all files are stored
SOURCE_DIR=/home/ec2-user/app-logs
mkdir -p $LOG_FOLDER
#check_root
if [ $USERID -ne 0 ]
then
echo -e "$R ERROR:: Please run this script with root access $N" | tee -a $LOG_FILE
exit 1 #give other than 0 upto 127
else
echo "You are running with root access" | tee -a $LOG_FILE
fi
VALIDATE() {
if [ $1 -eq 0 ]
then
echo -e " $M $2 is .... $G SUCCESS $N " | tee -a $LOG_FILE
else
echo -e " $M $2 is .... $R FAILURE $N " | tee -a $LOG_FILE
exit 1
fi
}
echo "Srcript started executed at $(date)"
# find command to find log file 14 days before one
FILES_TO_DELETE=$(find $SOURCE_DIR -name "*.log" -mtime +14)
while IFS= read -r filepath
do
echo "deleating $filepath" | tee -a $LOG_FILE
rm -rf $filepath
done <<< $FILES_TO_DELETE # from this input file <<< [ input sting ]
echo "script executed successfully $(date) done.."