2

I am looking for a simple way to constantly monitor a log file, and send me an email notification every time thhis log file has changed (new lines have been added to it).

The system runs on a Raspberry Pi 2 (OS Raspbian /Debian Stretch) and the log monitors a GPIO python script running as daemon.

I need something very simple and lightweight, don't even care to have the text of the new log entry, because I know what it says, it is always the same. 24 lines of text at the end.

Also, the log.txt file gets recreated every day at midnight, so that might represent another issue.

I already have a working python script to send me a simple email via gmail (called it sendmail.py)

What I tried so far was creating and running the following bash script:

monitorlog.sh

#!/bin/bash tail -F log.txt | python ./sendmail.py

The problem is that it just sends an email every time I execute it, but when the log actually changes, it just quits.

I am really new to linux so apologies if I missed something.

Cheers

  • Use this one-line cron job: `* * * * * find /path/to/file -mmin -1 -exec mail -s 'Subject here' email@example.com <<< 'Message here' \;` – rinogo Sep 27 '18 at 13:58

2 Answers2

1

You asked for simple:

#!/bin/bash

cur_line_count="$(wc -l myfile.txt)"
while true
do
    new_line_count="$(wc -l myfile.txt)"
    if [ "$cur_line_count" != "$new_line_count" ]
    then
        python ./sendmail.py
    fi
    cur_line_count="$new_line_count"
    sleep 5
done
DBedrenko
  • 4,871
  • 4
  • 38
  • 73
  • 1
    Simple is better! If it's a plain text file, then "wc -l" might be a more lightweight test of if the file has been modified... – Ian McGowan Mar 01 '18 at 22:44
  • @IanMcGowan That's a much better idea, performance wise, thanks! – DBedrenko Mar 01 '18 at 22:48
  • 1
    Yeah, but unless the log is huge, it probably is a matter of milliseconds. I've written scripts almost exactly like yours when it has to work on AIX, Linux, HPUX etc. If it's just Linux then the inotify approach is the most efficient of all. I'd never seen the incron package before, so I learned something new answering this question :-) – Ian McGowan Mar 02 '18 at 00:10
  • `wc -l filename` returns the number of lines and the file name,. If you want to extend this script and you only want the number of lines remember to use `wc -l < filename`. – Matt K Jan 29 '21 at 21:16
0

I've done this a bunch of different ways. If you run a cron job every minute that counts the number of lines (wc -l) compares that to a stored count (e.g. in /tmp/myfilecounter) and sends the emails when the numbers are different.

If you have inotify, there are more direct ways to get "woken up" when the file changes, e.g https://serverfault.com/a/780522/97447 or https://serverfault.com/search?q=inotifywait.

If you don't mind adding a package to the system, incron is a very convenient way to run a script whenever a file or directory is modified, and it looks like it's supported on raspbian (internally it uses inotify). https://www.linux.com/learn/how-use-incron-monitor-important-files-and-folders. Looks like it's as simple as:

sudo apt-get install incron
sudo vi /etc/incron.allow  # Add your userid to this file (or just rm /etc/incron.allow to let everyone use incron)
incron -e                  # Add the following line to the "cron" file
/path/to/log.txt IN_MODIFY python ./sendmail.py

And you'd be done!

Ian McGowan
  • 3,461
  • 3
  • 18
  • 23