?
Solved

Log File Management

Posted on 1999-06-24
11
Medium Priority
?
257 Views
Last Modified: 2013-12-27
Platform: SPARC 20
OS: Solaris 2.5.1
Shell: csh

I run a program with the following command to capture standard out and standard error:

program >>&! file.log &

Eventually, file.log gets big and I want to trim it down to the last 500 lines, or so. How can this be done without stopping the program?
0
Comment
Question by:denvermc
11 Comments
 

Expert Comment

by:yaiyai
ID: 2011188
I don't think you can truncate the file size while the file is opened.
You can tail -l 500 file.log > another file
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2011189
   tail -500 > other_file; cat other_file > file.log
depending on your shell (and/or OS), you may also try:
    (tail -500 file.log)|cat >file.log
0
 

Author Comment

by:denvermc
ID: 2011190
My experiments have shown that ahoffmann's suggestions will not work because the file is open, as yaiyai suggests. The approach which I have hit upon is to used |& rather than >>&!, to pipe it though another shell script which handles log file management.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 1

Expert Comment

by:jmohan
ID: 2011191
I guess ahoffmann's   (tail -500 file.log)|cat >file.log
suggestion should work even if the file is open for read/write.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2011192
jmohan, my suggestion may work, if
  1. the file is not opend using exclusive locks
  2. the shell allows this special construct
 
0
 
LVL 2

Expert Comment

by:GP1628
ID: 2011193
Not really possible the way you want it. You would have to stop the process, tail 500 lines to the file.log, then restart the process.

My suggestion is
tail -500 >archive.log
cp /dev/null file.log

Copying null to a log will zero it out without blowing off the processes that are writing to it. It lets you start the log over without having to stop and restart the process doing the logging.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2011194
> cp /dev/null file.log
is equivalent (not exactly the same) as
  echo "" >file.log
but still have the restrictions as decribed before (lock, etc.)
0
 

Author Comment

by:denvermc
ID: 2011195
Stopping the program is not an option. Further testing shows that ahoffmann's answer (tail -500 test.log)|cat >&! test.log works on Solaris with csh. However, sometimes it causes a core dump of the tail command. An examination of the core dump indicates that the tail command fails with:

     tail: cannot determine length of %s
0
 

Expert Comment

by:braxton95
ID: 2011196
have you tried logrotate?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2011197
>  tail: cannot determine length of %s
ok, this may occour if tail reads while your process writes to the file. AFAIK there is no way around this than copying the file first (probably not what you want).
Otherwise write a script which checks the result of the tail command, like (not ready for use):

 (tail -500 file.log)|cat >file.log
 if ($status != 0) then
    echo truncation failed with error: $status
 endif
 if (`wc file.log|awk '{print $1}'` < 500) then
    echo tail failed
 endif
0
 

Accepted Solution

by:
davidtg earned 200 total points
ID: 2011198
you'll have to use a temp file, like this

% tail -500 file.log > $TEMPFILE
% cat $TEMPFILE > file.log

because the output file (file.log) is often opened for writing at the time of instantiation, rather than after the input file (file.log -- oops!) is completely read


:-D
--

0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question