Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Log File Management

Posted on 1999-06-24
11
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

721 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