如何将日志文件的尾部传递给命令?

How do I pass the tail of a log file to a command?

我有很多长 运行 进程,我使用此命令在整个脚本的不同点给我发短信,并在某些事情完成时通知我:

curl http://textbelt.com/text -d number=<PHONE NUMBER> -d "message=Doneskiiz"

我想在消息中包含一个日志文件的尾部,而不是这条基本消息,这样我就可以看到最后几行以了解它是否成功并查看结果。如果可能的话,我希望包括当前时间(或在第二条消息中)。

步骤:

a) 执行"date" 以检索当前日期

b) 执行 "tail -n nnn" 以检索 "nnn" 日志文件的最后几行

c) 执行"curl" with "--data-binary @file" or "--data-binary @-" or "-F field=@filename" to post previous information .

您可以在 curl: pass a named parameter from stdin:

看到类似的例子
tail -n 20 my.log | curl -d date="$(date)" -d number="555123" -F log=@- "http://somewhere" 

我不知道这是否可以用 tail 和 pipes 来做,但它可以用 inotify 库来做。

看看 iwatch,它可以在您的文件和 运行 您的脚本被修改时触发一个事件。

读这个。

http://arkanis.de/weblog/2014-02-16-iwatch-run-a-command-when-a-file-changes