Tuesday, November 14, 2017

Maintain server logs

Maintain logs in very important thing. There are lot of ways to do this. Here I'm not going to do hard things. This is just a simple example of maintain server logs using a shell script.

Here I'm planing to do some tasks.
  1. Delete old log files
  2. Save last log file 
  3. Zip it and save in another place.
This can be used in your local server to maintain logs and it will be very easy because you need not to delete logs every time and save logs.

# Delete files

#file path for logs

#file path for eventspace default log

#files to be deleted

#Get the yesterday
yesterday=$(date -d '-1 day' '+%Y-%m-%d');

#get the full file name of last day server.log

cd $file_path;

#remove files in file_path

rm $file1
rm $file2
rm $file3
rm $file4

cd $file_path_eventspace;

if [ -s $lastLogFile ]
 #backup the last log file as zip
 tar -zcvf $lastLogFile.tar.gz $lastLogFile;

 #move file to var/logs/myAppLogs
 mv $lastLogFile.tar.gz /var/log/myAppLogs/
 #remove last log file
 rm $lastLogFile;
 echo "Last log file canot be found";


Sometimes it will be not easy to understand if you are a beginner. Let me explain.

There are two file paths.

  • file_path - Location of default log file located.
  • file_path_myApp - Location of application log file located

Then I've defined four file types that needs to be deleted. 

  • file1='catalina.2017-*';
  • file2='host-*';
  • file3='localhost*';
  • file4='manager*';

Then I get the date of yesterday because I need to to zip yesterday log file with the date as a file name (lastLogFile).

Then I remove files using rm command in Linux. 

  • rm $file1

Then I zip the yesterday file using tar -zcvf command.

  • tar -zcvf $lastLogFile.tar.gz $lastLogFile;

Then I move file to another location using mv command. 

  • mv $lastLogFile.tar.gz  /var/log/myAppLogs/

There are many file paths, you need to change all those file names with your locations. All commands are simple Linux commands. 

How to run a shell script file

Once you create your .sh file, you need to run it from terminal.
  • Go to your file location and right click and open terminal in same location( Otherwise you will need to change the path ).
  • Give file permission to the file.
                    chmod +x fileName.sh
  • Run your sh file.

Add a cronjob

Another special thing is you can add your script as a cronjob and it will run automatically according to the parameters which are given. For example if you need to run it in every minute, you can add following line to system crontab.

* * * * * /media/ravi/Data/Cronjob/deleteLogs.sh

Open you terminal and type crontab -e

Add your cronjob (Above mentioned line)

Save file.

  • Ctrl + X
  • Type y
  • Enter

You will be able to see like this.

You can change your cronjob with your requirements. 

No comments:

Post a Comment