Shell for regularly clearing logs on AIX

Source: Internet
Author: User

1. log files are regularly cleared on the AIX machine to release the disk space occupied by log files.

 

Shell does not seem to have a function to process date computing. It is very convenient to process date in Java, and it is too difficult for shell to process date. It is equal to writing an original date addition/subtraction function. Here we will consider specific applications and make some improvements based on the existing user code to calculate the date n days ago from the current date. Ksh implementation.

 

AIX 5.3 runs through

 

#! /Bin/KSh
Log_path =/home/utan/logs
Log_del_logfile =./logdel. Log
Keep_days = 9

# Set the current month day and year.
Month = 'date + % m'
Day = 'date + % d'
Year = 'date + % y'

# Get the yesterday
Getlastday ()
{
# Set the current month day and year.
# Month = 'date + % m'
# Day = 'date + % d'
# Year = 'date + % y'
Year = $1
Month = $2
Day = $3
 
# Add 0 to month. This is
# Trick to make month an unpadded integer.
Month = 'expr $ month + 0'
 
# Subtract one from the current day.
Day = 'expr $ day-1'
 
# If the day is 0 then determine the last
# Day of the previous month.
If [$ day-EQ 0]; then
 
# Find the preivous month.
Month = 'expr $ month-1'
 
# If the month is 0 then it is Dec 31
# The previous year.
If [$ month-EQ 0]; then
Month = 12
Day = 31
Year = 'expr $ year-1'

# If the month is not zero we need to find
# The last day of the month.
Else
Case $ month in
1 | 3 | 5 | 7 | 8 | 10 | 12) Day = 31 ;;
4 | 6 | 9 | 11) Day = 30 ;;
2)
If ['expr $ year % 4'-EQ 0]; then
If ['expr $ year % 400 '-EQ 0]; then
Day = 29
Fi
Else
Day = 28
Fi
;;
Esac
Fi
Fi

Case $ day
In 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9) Day = '0' $ day
Esac
Case $ month
In 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9) month = '0' $ month
Esac
# Echo $ year $ month $ day
}

# CaCl the date dividing value
Cacldividingvalue ()
{
Echo keeydays: $ keep_days
I = 1
While [$ I-Le $1]
Do
# Echo $ I
Getlastday $ year $ month $ day
I = $ ($ I + 1 ))
Done
}

# Delete the old logs before the keep days
Cacldividingvalue $ keep_days
Sdate = "$ year $ month $ day"
Echo the log files will be deleted before $ sdate.
For file in 'ls $ log_path'
Do
# Echo here $ File
If ["$ file"-lt "$ sdate"]; then
Echo $ log_path/$ File
Snow = 'date'
Echo [$ Snow] Delete $ log_path/$ FILE >>$ log_del_logfile
Rm-fr $ log_path/$ File
Fi
Done

 

 

2. Add the scheduled processing function, enter crontab-E, and add the following lines:

30 3 ***
/Home/scripts/logmonitor. Sh>/home/scripts/lcron. log 2> & 1

 

Run at every day
This script


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.