[SOLVED] Can I delete first few lines from a running log file?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can I delete first few lines from a running log file?
Hello,
This is RedHat 7 and I have a big log file of 12gb. Logs are constantly writing to it, so I can't rename file. This filesystem have only 4 gb space left, so I can't use logrotate also, for rotating 12gb file.
One solution I can think is, delete first half of lines (approx 32,231,354) and that would make this file size to 6gb.
Can someone suggest, how can I truncate older lines from this log file?
Do you have logrotate installed? If not, I'd look at the first.
logrotate will not work, because file size is 12gb and free space in this file-system is only 4gb. Not enough space to complete operation.
I don't want to truncate whole file, but only older half.
Can you USB-attach an external disk drive to this system, to give you the space that you need? Or install another internal one, then use LVM to add it to the storage pool to give you the additional space that you need?
Off the top of my head, I don't see any reason why you couldn't edit log file in the way you suggest, but I have no personal knowledge or experience doing so, so I hope that someone who knows more than I do joins this thread.
Get yourself a SystemRescue image on CD or usb stick. Boot with it, mount your normal root partition and do your edit. Then, when you reboot your main system, install and configure logrotate.
You cannot delete the initial part of a text file without creating another file, needing extra space.
Even compressing it would temporarily need space for both uncompressed and compressed files.
A truncation from the end is possible, but not desired here.
I would try another filesystem.
Say a
Code:
df /opt /var/log
shows that /opt is a different file system and has 7G avail. Then do
Code:
sed '1,32231354 d' /var/log/drtmessages > /opt/drtmessages.tmp
Directly copying this back is not recommended: concurrent writes can cause data corruption.
Better truncate the busy file:
Code:
: > /var/log/drtmessages
and check after a pause that its size is reduced.
Then move the tmp file back, but to a new name:
This filesystem have only 4 gb space left, so I can't use logrotate also, for rotating 12gb file.
You can use logrotate, since it can be configured to compress the old version, and can reduce log files by 80-90%, so - depending on what's in the log - could easily produce a file under 2gb.
Also logrotate defaults to same directory, but can be configured to put files elsewhere - i.e. a different disk, or perhaps to RAM - again, assuming the contents of the logs are actually needed.
Though...
Quote:
Originally Posted by james000
This is RedHat 7
Red Hat 7 is over twenty years old. You presumably mean RHEL 7, and if so why aren't you making use of the support contract you're paying for?
(Also, given RHEL 7 is EOL in under 9 months time, there should be a plan/process in place for migrating away from it...)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.