LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-26-2013, 05:43 PM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
Question delete content(truncate) of a log file in use


I have a 142MiB log file named c3600_router_log.txt under /tmp directory which is used by a process with PID 12369(Dynamips):

Code:
root@potato:/tmp# lsof | grep c3600_router_log.txt
dynamips  12369        root   15w      REG                8,8  199015029         76 /tmp/c3600_router_log.txt
root@potato:/tmp#
I would like to delete the content of this log file. If I do "> c3600_router_log.txt", then file is not truncated:

Code:
root@potato:/tmp# date "+%T"; ls -lh c3600_router_log.txt
02:42:22
-rw-r--r-- 1 root root 190M 27. veebr 02:42 c3600_router_log.txt
root@potato:/tmp# date "+%T"; > c3600_router_log.txt
02:42:24
root@potato:/tmp# date "+%T"; ls -lh c3600_router_log.txt
02:42:25
-rw-r--r-- 1 root root 190M 27. veebr 02:42 c3600_router_log.txt
root@potato:/tmp#
Just to be very sure that file is not rewritten to disk, I ran iotop -b -d 0.5 -p 12369 during flushing the content of /tmp/c3600_router_log.txt, but there was nearly no I/O usage.

How to explain this behavior? Are there situations where one is not able to flush the content of the file which in use by a process?
 
Old 02-26-2013, 05:54 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
It could have a file lock but you should see that as an error.

Try doing an echo "" > /tmp/c3600_router_log.txt and see if any error is reported.
 
Old 02-26-2013, 09:00 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by m4rtin View Post
How to explain this behavior?
The file is held open on the /proc/12369/15 file descriptor. If the process understands SIGHUP then clean the log and send the signal if it doesn't you have the choice of restarting the process or swapping files on the file descriptor (http://users.linpro.no/ingvar/fdswap.sh.txt).
 
Old 02-27-2013, 04:24 AM   #4
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Kustom42 View Post
It could have a file lock but you should see that as an error.

Try doing an echo "" > /tmp/c3600_router_log.txt and see if any error is reported.
Unfortunately this has exactly the same affect as "> c3600_router_log.txt", "cat /dev/null > c3600_router_log.txt" or ">| c3600_router_log.txt".


Quote:
Originally Posted by unSpawn View Post
The file is held open on the /proc/12369/15 file descriptor.
Yes.

Quote:
Originally Posted by unSpawn View Post
If the process understands SIGHUP then clean the log and send the signal if it doesn't you have the choice of restarting the process or swapping files on the file descriptor (http://users.linpro.no/ingvar/fdswap.sh.txt).
How can I tell if process understands SIGHUP signal? Should I just try "kill -s SIGHUP 12369"? In addition, what is the association between SIGHUP signal and log-file not being truncated?
 
Old 02-27-2013, 07:03 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by m4rtin View Post
How can I tell if process understands SIGHUP signal?
Generally speaking its manual page or documentation will tell you. As an example see your systems syslogd man page.


Quote:
Originally Posted by m4rtin View Post
In addition, what is the association between SIGHUP signal and log-file not being truncated?
Generally speaking processes accepting that signal close open file descriptors, re-read their configuration files and open file descriptors. Common usage for that is rotating log files.

*Apart from using http://users.linpro.no/ingvar/fdswap.sh.txt, depending on what logging you actually need to see, you may also choose to configure your 3600 to log to a remote syslog server.
 
1 members found this post helpful.
  


Reply

Tags
file descriptor, open file, users.linpro.no fdswap.sh



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Backup then truncate log file PsYc0TIC Linux - Desktop 13 10-17-2011 07:54 PM
File content delete ajkannan83 Linux - Software 4 07-01-2008 04:16 AM
how to backup & truncate the log file while the process is running mvenkat_in Programming 13 10-30-2007 07:03 AM
delete file-content ovince Programming 6 04-17-2007 03:44 AM
delete content form a file bru Programming 1 04-30-2004 07:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration