Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-14-2007, 03:25 AM
|
#1
|
|
LQ Newbie
Registered: Feb 2007
Posts: 8
Rep:
|
how to backup & truncate the log file while the process is running
Hi all,
I am in urgent need of a solution for my below problem. My problem definition is as follows:
I have an application (or process) which runs forever and writes the log the output into a log file say X.log. This log file size is becoming huge. So I want to take a backup of this X.log file into another file say Xbackup.log and truncate the X.log while the process is running and without hampering the process. The X.log file can't be moved or deleted because the process always keep on logging into this file.So that I can take the backup of the original file into some other place and reduce the original file size by truncating it.
Can anyone please suggest me or give me the script to my above problem.
Thanks in Advance,
Venkatesh M
|
|
|
|
10-14-2007, 05:16 AM
|
#2
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
This is on my system using "logrotate". You system may even be using it.
|
|
|
|
10-14-2007, 05:34 AM
|
#3
|
|
Moderator
Registered: May 2001
Posts: 24,827
|
If the process keeps the log open you may not be able to truncate it in place?
|
|
|
|
10-14-2007, 07:49 AM
|
#4
|
|
LQ Newbie
Registered: Feb 2007
Posts: 8
Original Poster
Rep:
|
Yeah,You are right.The process keeps the log file open writes into it most of the time.Because the process runs 24*7.
So how can I solve this problem.
Will logrotate or logadm (in solaris) solve this problem?
|
|
|
|
10-14-2007, 11:43 AM
|
#5
|
|
Moderator
Registered: May 2001
Posts: 24,827
|
A logrotate post-script still would need to -HUP the process to make it see the changed descriptor stuff.
You said "writes into it most of the time". Could you be more specific why the app can't be restarted? Would be easiest.
|
|
|
|
10-14-2007, 12:57 PM
|
#6
|
|
Senior Member
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247
Rep:
|
It's not the best sollution (probably) but you could use some db, which allows multiple access at the same time.
|
|
|
|
10-14-2007, 10:06 PM
|
#7
|
|
LQ Newbie
Registered: Feb 2007
Posts: 8
Original Poster
Rep:
|
Hi
The process can not be restarted or disturbed because its a very crucial application running in a production servers.So I should rotate the logs without disturbing the process.
Can I try like this?
1) Take a backup of original file ( cp X.log /backup/X.log)
2) Truncate the original file ( truncate X.log)
Will this work if the process is busy with writing into X.log?
|
|
|
|
10-14-2007, 11:39 PM
|
#8
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,026
|
Use the logrotate util as mentioned: it'll do all that stuff for you, if properly invoked.
|
|
|
|
10-15-2007, 07:46 AM
|
#9
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
I would open a new file descriptor with a second file then dup2 to the log descriptor:
Code:
int log_file = open("x.log", O_RDWR | O_CREAT | O_TRUNC, 0600);
/*... write to file ...*/
/*other threads may continue to write to log_file during this process*/
int new_log_file = open("x2.log", O_RDWR | O_CREAT | O_TRUNC, 0600);
if (dup2(new_log_file, log_file) < 0) /* error! */;
if (new_log_file != log_file) close(new_log_file);
rename("x.log", "x.log~");
rename("x2.log", "x.log");
ta0kira
|
|
|
|
10-19-2007, 12:01 AM
|
#10
|
|
LQ Newbie
Registered: Feb 2007
Posts: 8
Original Poster
Rep:
|
Hi
I can not do this in the application. I need to write an external script to backup the existing file and truncate it to 0 bytes. I have tried small script like below
cp X.log /backup/X.log1
> X.log
but the second truncate is not working properly. The file is getting truncated to 0 bytes but as soon as the application writes into the file, the file is coming again the previous size.
Can any one suggest..
|
|
|
|
10-19-2007, 07:08 AM
|
#11
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
The application needs to write to the file in append mode, otherwise it won't work. The application never closes the file, so its write index doesn't reset to 0 when the file is truncated. That means if the program last wrote at 1024 it will write at 1025 the next time, restoring the file size.
If the program opens the file with O_APPEND it should always write to the end regardless of other programs changing the file. That's how multiple processes can all write to the same file without tripping over each other.
ta0kira
|
|
|
|
10-20-2007, 04:07 AM
|
#12
|
|
LQ Newbie
Registered: Feb 2007
Posts: 8
Original Poster
Rep:
|
Hi,
Thanks a lot for your correct information.
I will try to solve this problem in any another way.
Thanks a lot
|
|
|
|
10-23-2007, 11:55 PM
|
#13
|
|
LQ Newbie
Registered: Feb 2007
Posts: 8
Original Poster
Rep:
|
Hi,
I could resolve this problem.
Actually the application was opening the log in WRITE mode. The Append mode was disabled in the configuration file (log4j.xml for Oracle App Server).
I enabled the APPEND mode and its working fine now.
Thanks all for your valuable suggestions.
|
|
|
|
10-30-2007, 07:03 AM
|
#14
|
|
LQ Newbie
Registered: Oct 2007
Posts: 1
Rep:
|
You can do something like this with copytruncate option in logrotate. See man logrotate for details.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:58 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|