views:

43

answers:

1

Hello everybody, I'm currently watching my log files like this tail -f and every now and then I press up key and hit return so new changes in log print into console, how to make it print itself when change in log file occur? Here is the requirement :

START loop
1. Check file.log
2. If file.log has changed print the changes
3. else print nothing
END

I wrote something similar for windows to execute from java :

import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

class LogWatch {
    public static void main(String args[]) {
        try {

            FileInputStream fstream = new FileInputStream("C:\\file.log");

            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String line;

            while (true) {

                line = br.readLine();
                if (line == null) {
                    Thread.sleep(500);
                } else {
                    System.out.println(line);
                }

            }

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

Which is unnecessary overhead, because this can be done from shell script, does anyone have any suggestions how to do it ?

+4  A: 

Use -F instead of -f

Assuming that -f isnt' working because you are hitting logrotate and not mentioning having to kill the previous instance of tail.

 -f      The -f option causes tail to not stop when end of file is
         reached, but rather to wait for additional data to be appended to
         the input.  The -f option is ignored if the standard input is a
         pipe, but not if it is a FIFO.

 -F      The -F option implies the -f option, but tail will also check to
         see if the file being followed has been renamed or rotated.  The
         file is closed and reopened when tail detects that the filename
         being read from has a new inode number.  The -F option is ignored
         if reading from standard input rather than a file.
David Dorward