Something that I'd like to share with you!

Wednesday, July 28, 2010

Using rotatelogs to rotate nohup.out

No comments :
Extending my focus on rotatelogs, I was thinking of to use them with other software. This might help me to on how to rotate nohup.out without using cronjob. First, I need something to throw STDOUT. Something simple as below;

while [ 1 ]
echo `date`
sleep 2

Script above will run in infinite loop and echo date output to STDOUT every 2 secs. Next is to test it with nohup so it could survive logout.

username@myserver:/mydir $ nohup ./test.sh
nohup: appending output to `nohup.out'

Let it run for a couple of seconds and kill it by CTRL-C. Then verify that the nohup.out contains couple lines of date in it. Next step is to pipe it with rotatelogs and observe what happen

username@myserver:/mydir $ nohup ./test.sh | ./rotatelogs nohup.out.%S 10 &
[1] 24828

Command line above will pipe the STDOUT from the simple test.sh and send them to rotatelogs to be rotated accordingly. nohup.out.%S will cause the nohup.out filename to rotate as below (%S: 2-digit second from rotatelogs)

-rw-r--r-- 1 username usergrp 1479 2010-07-28 10:32 nohup.out.40
-rw-r--r-- 1 username usergrp 1595 2010-07-28 10:32 nohup.out.50
-rw-r--r-- 1 username usergrp 1711 2010-07-28 10:33 nohup.out.00
-rw-r--r-- 1 username usergrp 1682 2010-07-28 10:33 nohup.out.10
-rw-r--r-- 1 username usergrp 1653 2010-07-28 10:33 nohup.out.20
-rw-r--r-- 1 username usergrp 1566 2010-07-28 10:33 nohup.out.30

Oops! Now I relize that the output file will never be replaced. It keep on appending to an old file. Meaning that it will grow and need to be manually archive. :-(

Looking forward to try logrotate and it will be my next post, soon.

Tuesday, July 27, 2010

Using rotatelogs to manage Logs in Apache (xampp)

No comments :
While performing a routine XAMPP (under Linux) healthy check, I've found out some log files had grown up to about 8GB. This might cause the system to slow down. Searching for a solution from the net lead me to this, rotatelogs. To use this rotatelogs it needs to be piped in the XAMPP httpd.conf which is located at /opt/lampp/etc/httpd.conf. So I comment out the 2 original lines and add 2 lines with rotatelogs piped to them like below;

#ErrorLog logs/error_log
ErrorLog "|bin/rotatelogs logs/error_log 5M"
#CustomLog logs/access_log common
CustomLog "|bin/rotatelogs logs/access_log 5M" common

Next is to restart the XAMPP by doing ./lampp restart (XAMPP for Linux). Unfortunately it failed and when I observed the error.log it shows;

piped log program 'bin/rotatelogs... failed unexpectedly

Maybe the path is wrong. Next try is to use full path by replace those rotatelogs piped lines with below;

#ErrorLog logs/error_log
ErrorLog "|/opt/lampp/bin/rotatelogs /opt/lampp/logs/error_log 5M"
#CustomLog logs/access_log common
CustomLog "|/opt/lampp/bin/rotatelogs /opt/lampp/logs/access_log 5M" common

Finally it works. No error in error_log found. New log file will be created whenever it reach 5MB size. Whenever it switch to the new log file, a unixtime will be appended to the log file name like below;

-rw-r--r-- 1 root root 22713 Jul 27 10:10 access_log.1280196529
-rw-r--r-- 1 root root 4989 Jul 27 10:21 access_log.1280197231
-rw-r--r-- 1 root root 725 Jul 27 10:20 error_log.1280197229

Log file size above is not 5MB yet but when I restart XAMPP, new log file is created. But I'm thinking, what if I don't want to keep all logs, just rotate them weekly. So I try this;

#ErrorLog logs/error_log
ErrorLog "|/opt/lampp/bin/rotatelogs /opt/lampp/logs/error_log.%a 86400"
#CustomLog logs/access_log common
CustomLog "|/opt/lampp/bin/rotatelogs /opt/lampp/logs/access_log.%a 86400" common

The new log file name now looks something like below.

-rw-r--r-- 1 root root 625 Jul 27 12:04 error_log.Tue
-rw-r--r-- 1 root root 1941 Jul 27 12:04 access_log.Tue

They will rotate in 24 hours (86400 secs) with a 3-character weekday name appended to them. I assume they will rotate and replace just like I plan in 1 week time, I hope so. :-)