Skip to ContentSkip to Footer
Start of content
- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - Linux
- >
- System Administration
- >
- log rotate script for linux
System Administration
Turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
Showing results for
-
- Forums
-
Blogs
- Alliances
- Around the Storage Block
- Behind the scenes @ Labs
- HPE Careers
- HPE Storage Tech Insiders
- Infrastructure Insights
- Inspiring Progress
- Internet of Things (IoT)
- My Learning Certification
- OEM Solutions
- Servers: The Right Compute
- Shifting to Software-Defined
- Telecom IQ
- Transforming IT
- Infrastructure Solutions German
- L’Avenir de l’IT
- IT e Trasformazione Digitale
- Enterprise Topics
- ИТ для нового стиля бизнеса
- Blogs
-
Quick Links
- Community
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Contact
- Email us
- Tell us what you think
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Enterprise.nxt
- Marketplace
- Aruba Airheads Community
-
Forums
-
Blogs
-
InformationEnglish
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
03-11-2008 07:50 AM
03-11-2008 07:50 AM
Hello all,
Need some help in writing a custom script to rotate my /var/log/messages files in my Linux server.
Instead of the syslog rotating my /var/log/messages weekly, I would like to rotate the file in a daily basis and save file like day1, day2... in my /var/log directory.
My /var/log directory would look like /var/log/day1 /var/log/day2 instead of /var/log/messages /var/log/messages.1
Please let me know how to establish this?
Thanks in advance
Adithyan
Need some help in writing a custom script to rotate my /var/log/messages files in my Linux server.
Instead of the syslog rotating my /var/log/messages weekly, I would like to rotate the file in a daily basis and save file like day1, day2... in my /var/log directory.
My /var/log directory would look like /var/log/day1 /var/log/day2 instead of /var/log/messages /var/log/messages.1
Please let me know how to establish this?
Thanks in advance
Adithyan
Keen to learn HP UX
Solved! Go to Solution.
2 REPLIES
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
03-11-2008 09:02 AM
03-11-2008 09:02 AM
Solution
First, set the respective line in /etc/syslog.conf to point to /var/log/day1 instead of to /var/log/messages;
then copy the following to the file,make it executable and schedule it to run on a daily basis (assuming you want to have only 5 copies):
#!/bin/bash
maxdays=5;rm -f day${maxdays} ; for i in `seq 1 ${maxdays}`; do num=`expr ${i} - ${maxdays}`;num=`expr ${num} \* -1`;echo "$num";newnum=`expr ${num} + 1`; if [ -f day${num} ]; then mv day${num} day${newnum};fi; done ; touch day1
And most important - don't forget to assign points.
then copy the following to the file,make it executable and schedule it to run on a daily basis (assuming you want to have only 5 copies):
#!/bin/bash
maxdays=5;rm -f day${maxdays} ; for i in `seq 1 ${maxdays}`; do num=`expr ${i} - ${maxdays}`;num=`expr ${num} \* -1`;echo "$num";newnum=`expr ${num} + 1`; if [ -f day${num} ]; then mv day${num} day${newnum};fi; done ; touch day1
And most important - don't forget to assign points.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
03-11-2008 06:01 PM
03-11-2008 06:01 PM
Re: log rotate script for linux
Here is my script that is used for rotating the listener log of Oracle.
Put it into crontab so that be executed at the fixed period.
===============================================
#!/usr/bin/bash
# lsnrlog-rotate.sh - Automatical rotate the listener.log
# History:
# 2008/2/12(alang)
#
. $HOME/.profile
LOGDIR="$ORACLE_HOME/network/log/"
LOGNAME="listener.log"
LSNR_CTL="$ORACLE_HOME/bin/lsnrctl"
DURATION=3
BK_LOG1="${LOGNAME}.1"
BK_LOG2="${LOGNAME}.2"
BK_LOG3="${LOGNAME}.3"
BK_LOG4="${LOGNAME}.4"
echo "**** $(date +'%y/%m/%d@%H:%M') ****"
if [ ! -w $LOGDIR -o ! -x $LOGDIR ]; then
echo "$0: you don't have the appropriate permission in $LOGDIR" >&2
exit 1
fi
cd $LOGDIR
if [ -f "$BK_LOG1" ];then
if [ -z $(find "$BK_LOG1" -mtime +$DURATION -print 2> /dev/null) ]
then
echo "$LOGNAME's most recent backup is more recent then $DURATION days: exiting"
exit 0
fi
fi
echo "Rotating log $LOGNAME (using a $DURATION day schedule)"
if [ -f "$BK_LOG3" ]; then
echo "... $BK_LOG3 -> $BK_LOG4"; mv -f "$BK_LOG3" "$BK_LOG4"
fi
if [ -f "$BK_LOG2" ]; then
echo "... $BK_LOG2 -> $BK_LOG3"; mv -f "$BK_LOG2" "$BK_LOG3"
fi
if [ -f "$BK_LOG1" ]; then
echo "... $BK_LOG1 -> $BK_LOG2"; mv -f "$BK_LOG1" "$BK_LOG2"
fi
if [ -f "$LOGNAME" ]; then
$LSNR_CTL set log_status off
cp -f "$LOGNAME" "$BK_LOG1"
cat /dev/null > "$LOGNAME"
$LSNR_CTL set log_status on
echo "... $LOGNAME -> $BK_LOG1"
fi
#EOF
Put it into crontab so that be executed at the fixed period.
===============================================
#!/usr/bin/bash
# lsnrlog-rotate.sh - Automatical rotate the listener.log
# History:
# 2008/2/12(alang)
#
. $HOME/.profile
LOGDIR="$ORACLE_HOME/network/log/"
LOGNAME="listener.log"
LSNR_CTL="$ORACLE_HOME/bin/lsnrctl"
DURATION=3
BK_LOG1="${LOGNAME}.1"
BK_LOG2="${LOGNAME}.2"
BK_LOG3="${LOGNAME}.3"
BK_LOG4="${LOGNAME}.4"
echo "**** $(date +'%y/%m/%d@%H:%M') ****"
if [ ! -w $LOGDIR -o ! -x $LOGDIR ]; then
echo "$0: you don't have the appropriate permission in $LOGDIR" >&2
exit 1
fi
cd $LOGDIR
if [ -f "$BK_LOG1" ];then
if [ -z $(find "$BK_LOG1" -mtime +$DURATION -print 2> /dev/null) ]
then
echo "$LOGNAME's most recent backup is more recent then $DURATION days: exiting"
exit 0
fi
fi
echo "Rotating log $LOGNAME (using a $DURATION day schedule)"
if [ -f "$BK_LOG3" ]; then
echo "... $BK_LOG3 -> $BK_LOG4"; mv -f "$BK_LOG3" "$BK_LOG4"
fi
if [ -f "$BK_LOG2" ]; then
echo "... $BK_LOG2 -> $BK_LOG3"; mv -f "$BK_LOG2" "$BK_LOG3"
fi
if [ -f "$BK_LOG1" ]; then
echo "... $BK_LOG1 -> $BK_LOG2"; mv -f "$BK_LOG1" "$BK_LOG2"
fi
if [ -f "$LOGNAME" ]; then
$LSNR_CTL set log_status off
cp -f "$LOGNAME" "$BK_LOG1"
cat /dev/null > "$LOGNAME"
$LSNR_CTL set log_status on
echo "... $LOGNAME -> $BK_LOG1"
fi
#EOF
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
End of content
United States
Hewlett Packard Enterprise International
Communities
- Communities
- HPE Blogs and Forum
© Copyright 2019 Hewlett Packard Enterprise Development LP