1847639 Members
3909 Online
110265 Solutions
New Discussion

Re: tail script help ..

 
someone_4
Honored Contributor

tail script help ..

Ok here we go ..
I have a script that does a
tail -f progmon.log | grep ERROR
Tough a remsh. (progmon.log is a log from a 3rd party app). And our surveillance
center is watching this log 18 hours a day. Now I have a cron job that clears out the log every couple of hours. And what happeneds is that when the log clears the the tail will freeze up.And this kicks them out of the menu driven script. Is there another way to do the tail or clear the log so it does not freeze up?

Richard
16 REPLIES 16
Patrick Wallek
Honored Contributor

Re: tail script help ..

How are you clearing the log?

The thing that first comes to mind to do is:

# cat /dev/null > progmon.log

or

# > progmon.log

(Those 2 commands do the same thing).

But since the tail process has the file open, I don't know if you'd regain the disk space when you empty the file like that.
Roger Baptiste
Honored Contributor

Re: tail script help ..

<>

What command are you using for clearing the log?? rm is a no-no , since it deletes the
reference to the file (inode).

You can use,
#cp $logfile $logfile.$$
#>logfile

It should work fine.

-raj
Take it easy.
someone_4
Honored Contributor

Re: tail script help ..

Here is the script that runs of cron:
cd /var/simp/logs
for FILE in *.log
do
> $FILE
# sleep 2
done

James R. Ferguson
Acclaimed Contributor

Re: tail script help ..

Hi Richard:

If you are clearing the log by removing and recreating the file, then this will certainly fail. Redirect /dev/null into the log file to clear it.

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: tail script help ..

Hmmmmmm.......

I just did a test that is almost exactly like what you are doing, and doing the '> filename' do screw up the tail. I don't understand why, but it does.

Now I'll have to think about this.

Re: tail script help ..

Yes this is a problem with tail -f when logs are cleared down. Why not do something like this...

while true
do
tail -f progmon.log
sleep 10
done

Then change your script which clears the logs to:

cd /var/simp/logs
for FILE in *.log
do
fuser -k $FILE
> $FILE
# sleep 2
done

A bit messy I know, but it should work.

HTH

Duncan

I am an HPE Employee
Accept or Kudo
James Beamish-White
Trusted Contributor

Re: tail script help ..

Hmmmm, interesting thought.... what happens when you cat /dev/null into an open piped file? I have no idea... maybe you should try using "cat /dev/null > ..." in your script in case the ">" is interpreted differently in a script rather than at the shell prompt.

Also, have you though of creating a script that first kills the tail -f, then zeros the logfile, then restarts the tail -f (with nohup and &)?

Cheers,
James
GARDENOFEDEN> create light

Re: tail script help ..

Ooops!

Just thought about this a bit more, and this won't work as it will terminate whatever process is writing to the logs. Of course you could still use fuser to determine the processes that have the file open, and then determine which one is the tail -f using ps.

Just goes to show you should think before you open your mouth!


I am an HPE Employee
Accept or Kudo
Patrick Wallek
Honored Contributor

Re: tail script help ..

OK. Try this to clear your logfiles. I think it will work for you. I have tried it and it does 0 out the log file and it does NOT kill the tail process. It does take tail a bit (15-30 seconds -- the last time it took about a minute) to come back though.

sed 'd' $FILE > $FILE

The 'd' in the sed command (the single quotes are required) tells it to delete all lines of the file.

Hope this helps.
A. Clay Stephenson
Acclaimed Contributor

Re: tail script help ..

Hi Richard:

You remember that stuff about traps. This would be an elegant solution to your problem.
Suppose that your cron job that copies /dev/null to your log or however you wish to clear the log first sends a signal to the your tail script process(es); then sleeps a bit to allow the processes to respond; and then clears your log. Your tail script processes would then respond by sleeping a bit and then restarting themselves and don't forget to reassert the trap.

I could code this for you but it's the journey that's important.

Regards, Clay
If it ain't broke, I can fix that.
Deepak Extross
Honored Contributor

Re: tail script help ..

Richard,

How about running 'tail' in a loop, rather than relying on 'tail -f'?

Maybe something like
while true
do
tail -24
sleep 1
done

Just replace 24 with the number of lines your screen can display.

Cheers,
Deepak.
Robin Wakefield
Honored Contributor

Re: tail script help ..

Hi Richard,

The GNU tail does this perfectly. Can be obtained from:

http://hpux.connect.org.uk/hppd/hpux/Gnu/textutils-2.0.10

Rgds, Robin.
Deepak Extross
Honored Contributor

Re: tail script help ..

Running tail in a loop (as mentioned in my last post) works fine as long as:
1. the log file is not 'rm'ed
2. no more than 24 lines (one screenfull) are written per second.

You could increase the number of lines from 24 to anything you're comfortable with, but with larger values the display tends to be a little jerky.
Wodisch
Honored Contributor

Re: tail script help ..

Deepak: if you put your loop into parentheses and redirect their output to "grep" it should work:

( while :; tail -f $LOG; done ) | grep ...

HTH,
Wodisch
Deepak Extross
Honored Contributor

Re: tail script help ..

Richard,

Just curious..how did you work around this one?
someone_4
Honored Contributor

Re: tail script help ..

Hey Deepak and everyone ..
I am still playing with this .. I have had other issues come up that need to be taken care of. I will assign points and post results when I get a chance.

Richard