Hi,

i want to truncate the log file which is already in use ( log is getting appended) . After that i want to make sure log is still getting appeneded after performing truncate. I have been using below command for the same but don't see it is happening.

truncate -s 0 rosout.log

i belive it should truncate the log file contents and after that append the new one.
even it will be helpfull if i can specify how many contents i would be able to truncate.

truncate -s 1M rosout.log

Dani AI

Generated

Short summary and checklist specific to this thread: ’s file can be safely shrunk while a process still writes to it, but the behaviour depends on which inode the process has open, whether it opened the file with O_APPEND, and whether the file was preallocated. ’s suggestion to use logrotate is the usual long‑term solution; is right to flag filesystem/block/allocation differences — that affects what du vs ls -l report and whether disk space is actually freed.

Quick diagnostics (run these before and after you try anything):

lsof /full/path/to/rosout.log
stat -c "%n %s %i" /full/path/to/rosout.log
du -h /full/path/to/rosout.log
tail -F /full/path/to/rosout.log
  • lsof shows which PID has the file open (or if the writer is using a different path/inode).
  • stat shows the inode and logical size.
  • du shows actual disk blocks used.
  • tail -F follows the filename (useful when the file is renamed/rotated).

Practical rotation options and tradeoffs:

  • Use logrotate with copytruncate if you cannot restart or signal the writer. copytruncate copies current contents and truncates the same file so the writer keeps the same FD, but it can lose a small window of log lines.
  • Prefer rename + signal (rename the file, then send the writer whatever signal or command it needs to reopen logs) — safer and avoids races if the writer supports reopening.

Example logrotate snippet (put in /etc/logrotate.d/rosout and test with logrotate -d):

/full/path/to/rosout.log {
    size 1M
    rotate 7
    compress
    missingok
    notifempty
    copytruncate
}

If the writer supports it, replace copytruncate with a postrotate that sends a reopen signal to the daemon.

Caveats and gotchas:

  • If lsof shows the file as (deleted) or the process still holds the FD, truncating/renaming may not free space until the FD is closed.
  • Preallocated files (fallocate) or some filesystems may behave differently; du vs ls -l will help you spot that.

Follow the checklist above to confirm which method is safest for your rosout writer.

Recommended Answers

All 2 Replies

As I have said to others, use logrotate. See the man page for logrotate and logrotate.conf usage. That will let you do exactly what you want.

Also depending on how much you're changing the size, it would be possible to have a smaller file size and still use the same amount on the hard drive, due to the way your OS has structured the file system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.