Sponsored Content
Top Forums Shell Programming and Scripting "Customized" log rotate script - some advice please Post 302623697 by newbie_01 on Saturday 14th of April 2012 11:09:35 AM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
Mojo::Log(3pm)						User Contributed Perl Documentation					    Mojo::Log(3pm)

NAME
Mojo::Log - Simple logger SYNOPSIS
use Mojo::Log; # Log to STDERR my $log = Mojo::Log->new; # Customize log file location and minimum log level my $log = Mojo::Log->new(path => '/var/log/mojo.log', level => 'warn'); # Log messages $log->debug("Why isn't this working?"); $log->info("FYI: it happened again"); $log->warn("This might be a problem"); $log->error("Garden variety error"); $log->fatal("Boom!"); DESCRIPTION
Mojo::Log is a simple logger for Mojo projects. EVENTS
Mojo::Log can emit the following events. "message" $log->on(message => sub { my ($log, $level, @messages) = @_; ... }); Emitted when a new message gets logged. $log->unsubscribe('message'); $log->on(message => sub { my ($log, $level, @messages) = @_; say "$level: ", @messages; }); ATTRIBUTES
Mojo::Log implements the following attributes. "handle" my $handle = $log->handle; $log = $log->handle(IO::Handle->new); Log file handle used by default "message" event, defaults to opening "path" or "STDERR". "level" my $level = $log->level; $log = $log->level('debug'); Active log level, defaults to the value of the "MOJO_LOG_LEVEL" environment variable or "debug". These levels are currently available: "debug" "info" "warn" "error" "fatal" "path" my $path = $log->path $log = $log->path('/var/log/mojo.log'); Log file path used by "handle". METHODS
Mojo::Log inherits all methods from Mojo::EventEmitter and implements the following new ones. "new" my $log = Mojo::Log->new; Construct a new Mojo::Log object and subscribe to "message" event with default logger. "debug" $log = $log->debug('You screwed up, but that is ok'); Log debug message. "error" $log = $log->error('You really screwed up this time'); Log error message. "fatal" $log = $log->fatal('Its over...'); Log fatal message. "format" my $message = $log->format('debug', 'Hi there!'); my $message = $log->format('debug', 'Hi', 'there!'); Format log message. "info" $log = $log->info('You are bad, but you prolly know already'); Log info message. "is_level" my $success = $log->is_level('debug'); Check log level. "is_debug" my $success = $log->is_debug; Check for debug log level. "is_error" my $success = $log->is_error; Check for error log level. "is_fatal" my $success = $log->is_fatal; Check for fatal log level. "is_info" my $success = $log->is_info; Check for info log level. "is_warn" my $success = $log->is_warn; Check for warn log level. "log" $log = $log->log(debug => 'This should work'); Emit "message" event. "warn" $log = $log->warn('Dont do that Dave...'); Log warn message. SEE ALSO
Mojolicious, Mojolicious::Guides, <http://mojolicio.us>. perl v5.14.2 2012-09-05 Mojo::Log(3pm)
All times are GMT -4. The time now is 09:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy