"Customized" log rotate script - some advice please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "Customized" log rotate script - some advice please
# 1  
Old 04-14-2012
"Customized" log rotate script - some advice please

Hi,

Am trying to write my own version of a log rotate scripts 'coz I don't have the logrotate for other flavors of *nix servers. Probably should try and download the source and re-compile but our SA don't want to do so.

Anyway, just want to know if there is any way to improve on the code below:

Code:
 
            while [ ${rotate_max} -gt 0 ];
            do
                let rotate_next=${rotate_max}
                let rotate_prev=${rotate_max}-1
                if [[ ! -f ${rotate_file}.${rotate_next} ]] && [[ ${rotate_next} -ne 0 ]] ; then
                    touch ${rotate_file}.${rotate_next}
                fi
                if [[ ! -f ${rotate_file}.${rotate_prev} ]] && [[ ${rotate_prev} -ne 0 ]] ; then
                    touch ${rotate_file}.${rotate_prev}
                fi
                if [[ ${rotate_max} -eq 1 ]] ; then
                   echo "cp -p ${rotate_file} ${rotate_file}.${rotate_next}"
                else
                   echo "cp -p ${rotate_file}.${rotate_prev} ${rotate_file}.${rotate_next}"
                fi
                let rotate_max=${rotate_max}-1
            done

Sample output as below:

Code:
 
cp -p alert_db01.log.9 alert_db01.log.10
cp -p alert_db01.log.8 alert_db01.log.9
cp -p alert_db01.log.7 alert_db01.log.8
cp -p alert_db01.log.6 alert_db01.log.7
cp -p alert_db01.log.5 alert_db01.log.6
cp -p alert_db01.log.4 alert_db01.log.5
cp -p alert_db01.log.3 alert_db01.log.4
cp -p alert_db01.log.2 alert_db01.log.3
cp -p alert_db01.log.1 alert_db01.log.2
cp -p alert_db01.log alert_db01.log.1
$ ls -l alert*
-rw-r--r--  1 newbie01  guest  0 Apr 14 01:25 alert_db01.log
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.1
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.10
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.2
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.3
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.4
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.5
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.6
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.7
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.8
-rw-r--r--  1 newbie01  guest  0 Apr 14 10:35 alert_db01.log.9
$

rotate_max is a variable that will be passed as an argument or read from a config file. For the sample output above, rotate_max is at 10.

Am currenly doing echo cp at the moment, will change that to the actual cp when am happy with how the script is running.

BTW I have the section of code below 'coz the cp will give an error if the file to copy from does not exist.

Code:
 
                if [[ ! -f ${rotate_file}.${rotate_next} ]] && [[ ${rotate_next} -ne 0 ]] ; then
                    touch ${rotate_file}.${rotate_next}
                fi
                if [[ ! -f ${rotate_file}.${rotate_prev} ]] && [[ ${rotate_prev} -ne 0 ]] ; then
                    touch ${rotate_file}.${rotate_prev}
                fi

Any feedback much appreciated. Thanks in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

6. Shell Programming and Scripting

How to find a file which are not ends with ".zip" and which are ends with "*.log*" or "*.out*"?

I am new to bash/shell scripting. I want to find all the files in directory and subdirectories, which are not ends with “.zip” and which are contains in the file name “*.log*” or “*.out*”. I know below command to get the files which ends with “.log”; but I need which are not ends with this... (4 Replies)
Discussion started by: Mallikgm
4 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question