November 18, 2020 / Nirav S
11.222.333.44 – – [14/SEP/2020:11:01:28 –0600] “GET /blog/page-address.htm HTTP/1.1” 200 182 “-” “Mozilla/5.0 Chrome/60.0.3112.113”
Many times these log files occupy a considerable space in the server disk drive so we need to clean the log files periodically.
Navigate to /var/log there you will see many compressed file named as .tar.gz as an extension we need to clean this file
This will delete all the compressed files in the log folder
This is about how to clean apach2/nginx log
Navigate to the service folder whichever you are using apache2 or Nginx
This script will automatically delete the log files from the server at the specified time so that you need not do it manually every time you find that space is getting full.
Create a scripts directory
sudo mkdir /scripts
Navigate to the scripts directory
Create a new file inside the folder
sudo touch log-clean.sh
Edit the log-clean file
sudo vim log-clean.sh
Copy the below content in the log-clean file
#!/bin/bash sudo rm -rf /var/log/*.gz sudo rm -rf /var/log/apache2/*.gz
Now give the file executable permission
sudo chmod a+x log-clean.sh
You have to specify the time a which the log-clean should execute you can get the time via https://crontab.guru/
Edit the crontab file to schedule it
sudo crontab -e
In our case, we have scheduled it to every day at the start of the day
0 0 * * * sh /scripts/log-clean.sh
Save the file with Ctrl+X and press Y
By this, you can automate the log clean process which does free the huge space occupied by the log files and decreases the associated cost.
Have queries about your AWS project ideas and concepts? Please drop in your project details to discuss with our AWS experts, professionals and consultants.