我目前正在像这样查看我的日志文件日志文件何时发生变化?这是要求:
START loop
1. Check file.log
2. If file.log has changed print the changes
3. else print nothing
END
我为 windows 编写了类似的东西以从 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());
}
}
}
这是不必要的开销,因为这可以通过 shell 脚本完成,有没有人对如何做有任何建议?
最佳答案
使用-F
代替-f
假设 -f
不起作用,因为您正在点击 logrotate 而没有提到必须杀死前面的 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.
关于linux - 如何为日志创建 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2606297/