[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

truncate a file

I had asked this question previously but I closed out the question prematurely:

"Is there a way to truncate a file while it is open and being written to? I'm using an application that hasn't been set up with log file rolling and I'm trying to manange it externally."

I put togther a program that uses the truncate() function which I though was working the way I wanted to, but it turns out it won't truncate a file while another program has that file open. I even tried suspending the first process, truncating the file, and then resuming the suspended process. The file size was reduced, but after I resumed the process, the file was restored to the size it had before I suspended the process (argh).

Any input much appreciated,
Ollie


0
opike
Asked:
opike
6 Solutions
 
ozoCommented:
head file > temp
mv temp file

but any other writes that the process does to the file before closing it will be lost

0
 
ravenplCommented:
I guess in Your case it's impossible. Thats, becouse the original application haven't opened the file in APPEND mode (that's why it restores to the original size after truncate)...
In such case, the orig application (after resuming) writes to outside the file (at original position), which is fine under UNIX systems (the file grows and it's propably sparse file) :(
0
 
ahoffmannCommented:
> "Is there a way to truncate a file while it is open and being written to? ..
as long as the application has the file open, you can truncate it as ozo suggested, but that truncation will only be "visible" at user level, the filesystem still occupies the whole place.
So it's impossible to truncate the size of file from another process while it is in use.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gheistCommented:
> "Is there a way to truncate a file while it is open and being written to? I'm using an application that hasn't been set up with log file rolling and I'm trying to manange it externally."

If opening process only appends file then
: > file
(null command truncating and outputing to file)
will help

If program writes to file position you have to create a script that makes program reopen file after truncation.
0
 
ravenplCommented:
ahoffmann: unfortunatelly You wrong(at least concerning linux).
If the another process has the file open in O_APPEND mode, everything would work fine. Otherwise, on next write, the filesystem will create sparse file.
But anyway, after truncating, the file is truncated (also in filesystem meaning)
0
 
ahoffmannCommented:
ravenpl, sometimes we learn new things every day, thanks!
0
 
NopiusCommented:
opike, what OS/version are you using?
All above is correct, you CANNOT truncate open file externally.

Sometimes there are no direct ways of doing things (yes, it's your case), but some workarounds (dirty hacks) are possible :-)

I know, truncating of file, open by another process (it doesn't matter if it was opened with APPEND or WRITE mode) is _possible_.
You only need to fix some fields in one kernel table and their copies in userspace of running process.
With kernel space you may write your own syscall handler (yes, it's possible) :-)
With user process space you may also use new syscall or it might be possible to use ptrace() syscall.
If you are ready to go with all these difficulties (read: this truncation is _so_ importent for you), we will proceed.

Have a nice weekend.

Nopius
0
 
root_startCommented:
A way to "truncate" a file could be the following: cat /dev/null > <file>

I hope it helps.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now