Help

Welcome!

This community is for professionals and enthusiasts of our products and services.
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

0

Script to delete the log file after a given time

Avatar
Administrator

How could the script automatically delete the log files from the server at the specified time?

Avatar
Discard
1 Answer
0
Best Answer

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.

Step 1
Create a scripts directory
sudo mkdir /scripts
Step 2

Navigate to the scripts directory

cd /scripts
Step 3

Create a new file inside the folder

sudo touch log-clean.sh
Step 4

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/odoo/odoo-server.log


Step 5

Now give the file executable permission

sudo chmod a+x log-clean.sh

Create a cronjob for the file to be executed by its own

Step 6

You have to specify the time a which the log-clean should execute you can get the time via https://crontab.guru/

Step 7

Edit the crontab file to schedule it

sudo crontab -e
Step 8

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

Conclusion

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.


Avatar
Discard