linux - 如何为日志创建 shell 脚本

标签 linux shell

我目前正在像这样查看我的日志文件日志文件何时发生变化?这是要求:

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/

相关文章:

email - 如何从 shell 脚本通过邮件发送特殊字符?

c++ - 为什么我不能使用 EGL 创建 headless OpenGl 上下文?

c - 尝试从汇编程序(64 位)的 glibc 调用 C 函数

linux - 在 Bash 脚本中使用带有转义字符的 Awk

shell - 用于移动列表中指定文件的批处理命令

git - 从 git 提交中提取树 sha1

linux - Bash:通过忽略空格的定界符拆分字符串

Linux命令到DOS

linux - Raspberry Pi - 缺少 linux-headers 目录

我们可以在 make 中运行一个 python 脚本来生成一些 .c 和 .h 文件,然后构建一个镜像吗?