Need script to rotate logs


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need script to rotate logs
# 1  
Old 11-28-2013
Need script to rotate logs

I have few solaris-10 non global zones, where one application is writing some logs to /var/ovd/ConfigLogs. It keeps increasing all the time, as it is needed by application team as of now. I want a small script, which I can configure in cronjob, which should :
- Run every Saturday 10 PM
- Copy /var/ovd/ConfigLogs to /var/ovd/ConfigLogs.1
- Nullyfy /var/ovd/ConfigLogs
- bzip /var/ovd/ConfigLogs.1
- Next Saturday (Second cronjob), it should repeat, but should ot overwrite /var/ovd/ConfigLogs.1.bz
I am still trying to understand if logadm can help me in this. Can somebody help me for this script to put in cronjob ?
# 2  
Old 11-28-2013
Quote:
Originally Posted by solaris_1977
I have few solaris-10 non global zones, where one application is writing some logs to /var/ovd/ConfigLogs. It keeps increasing all the time, as it is needed by application team as of now. I want a small script, which I can configure in cronjob, which should :
- Run every Saturday 10 PM
- Copy /var/ovd/ConfigLogs to /var/ovd/ConfigLogs.1
- Nullyfy /var/ovd/ConfigLogs
- bzip /var/ovd/ConfigLogs.1
- Next Saturday (Second cronjob), it should repeat, but should ot overwrite /var/ovd/ConfigLogs.1.bz
I am still trying to understand if logadm can help me in this. Can somebody help me for this script to put in cronjob ?
I am no Solaris expert and do not know "logadm", but this should be fairly straightforward. Some things you have to keep in mind:

1. You should - under no circumstances - change the inode of the log, otherwise processes which have still opened this file (inode) would keep writing into it. You need to "zap" the file: replace its contents with a single "EOF"-marker, provided by "/dev/null". Do it this way:

Code:
cat /dev/null > /path/to/logfile

2. If you copy the file to another location this process needs time. In the meantime the process writing to the log might put something into it and you would lose that when you "zap" the file. You will have to decide how big this risk is (based on the frequency with which the logfile gets written - if there is one message per minute the risk of dropping a log message is practically negligeable, if it is 10 messages per second it is considerable). Note, though, that the risk never drops to zero, so if you need complete logs no matter what you will have to elaborate on the process.

3. How to configure a cron entry is in the manual, if you have problems with this i suggest you search for it here, there are examples galore. One thing you have to keep in mind when writing the cron job is the "Cron Error Number One".

I hope this helps.

bakunin
# 3  
Old 12-02-2013
I can write a simple script and set it up in cronjob, as below
Code:
/usr/bin/cp -p /var/ovd/ConfigLogs /var/ovd/ConfigLogs.1
cat /dev/null > /var/ovd/ConfigLogs
/usr/bin/bzip2 /var/ovd/ConfigLogs.1

But next time Saturday, it will overwrite /var/ovd/ConfigLogs.1. how should I prevent it ? I am not good in programming. It should create another files with extension of .1, .2, .3 and so on.
# 4  
Old 12-02-2013
You can do
Code:
LOG=/var/ovd/ConfigLogs
m=5
for n in 4 3 2 1
do
  [ -f $LOG.$n.bz2 ] && mv $LOG.$n.bz2 $LOG.$m.bz2
  m=$n
done
cp -p $LOG $LOG.1
> $LOG
bzip2 $LOG.1

---------- Post updated at 07:08 PM ---------- Previous update was at 06:29 PM ----------

A crontab entry logs like this
Code:
00 10 * * 5 /path/to/rotatescript

and can be created with e.g.
Code:
EDITOR=vi crontab -e

--
If you have root rights I would go for logadm. It is only one command
Code:
logadm -w /var/ovd/ConfigLogs -c -C 5 -p 1w -z 0 -P 'Sat Nov 02 10:00:00 2013'

and an entry is added to /etc/logadm.conf.
No crontab entry is needed, because the existing logadm entry handles all the /etc/logadm.conf.

Last edited by MadeInGermany; 12-02-2013 at 08:16 PM..
# 5  
Old 12-02-2013
Thanks. I tested it with current time and it worked. But logadm seems to be more effective. You command is good, but a small modification is needed here. Now apps team wants that all logs should be copied/compressed inside newly created directory - /var/ovd/Config-Backup-Logs instead of /var/ovd.
How should I get it configured in /etc/logadm.conf ?
# 6  
Old 12-03-2013
It is possible, but they are paying you - not me...
In the script you can change the variable LOG to only the base filename, and prefix the directories in the mv and test command, and maybe even modify the bzip2 command. But actually this is very unusual - all logrotate-like commands default to keep the backups in the same directory.
Regarding the logadm, the -t template option might be suitable.
More details about logadm and bzip2 are in the man pages:
Code:
man logadm
man bzip2

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. UNIX for Dummies Questions & Answers

Logs do not rotate

My problem: Both access and error logs do not rotate any more and get really large. They are located here: /srv/www/+vHost name here+/logs/ Configuration seems to be here: /etc/logrotate.conf => looks OK, including "size 10M" to avoid large files (/etc/logrotate.d => is empty) manually... (4 Replies)
Discussion started by: floko
4 Replies

3. Shell Programming and Scripting

Script to rotate file log

Hi Experts, I have script on crontab and give output quite large. I would like to know how to create rotate log when the size of log maximum 50MB if the test.log is 50MB then create test.0 Thanks Edy (2 Replies)
Discussion started by: edydsuranta
2 Replies

4. Shell Programming and Scripting

Help with a rotate log script

Hi all, Am trying to write my own log rotate script. Curremtly, what I have is as below: #!/bin/ksh file_to_rotate=${1} x=${2} while ] do let curr=${x} let prev=${x}-1 if ] ; then #echo "cp -p ${file_to_rotate} ${file_to_rotate}.${curr}" cp -p... (7 Replies)
Discussion started by: newbie_01
7 Replies

5. UNIX for Dummies Questions & Answers

Rotate logs every 1 hour

Hello All, I am learning unix and basically I want to rotate one of my application logs every 1 hour. I need to rotate that file every one hour. I looked in the forums and googled.. but couldn;t get proper information. Requesting you all to kindly guide me. Our application is running on... (4 Replies)
Discussion started by: arunpvp
4 Replies

6. Shell Programming and Scripting

cron script to rotate log files

I have a mac server. I have been having problems with my logs. My hard disk became full, when i researched into why it was full it was due to massive log files. There was barley any log rotation policies in place on the server. I tired to use logrotate. This doesn't work on my server. It is a MAC... (19 Replies)
Discussion started by: timgolding
19 Replies

7. Shell Programming and Scripting

Script for Log Rotate

Hello, I only know the basic for shell programing. I need help for this, I thinks this is a basic for anyone who know a litle of shell scripting. I need creat a script for a rotatate logs, when a filesystem is full. I have a filesystem. The rotate consist in zip the current log (copy) and... (1 Reply)
Discussion started by: El Rengo
1 Replies

8. Shell Programming and Scripting

Perl script to rotate logs

I have a shell script that will gzip/tar/archive application logs that are over 20 days old which works just fine, but I would like to convert to a Perl script. Problem is, I'm a beginner with Perl and all attempts so far have failed. Basicaly I have a log dir /app/logs that contains several... (18 Replies)
Discussion started by: theninja
18 Replies

9. HP-UX

Check size and rotate log script.

Hi Can you suggest some perl script. My OS is HP-UX 11.11 I want to it into a cron job. Every night it will backup the file with that day's date and open a dummy file. Thanks Ash (3 Replies)
Discussion started by: ashishT
3 Replies
Login or Register to Ask a Question