Recent

Author Topic: Some sort of verry simple file monitor tool.  (Read 590 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 886
Some sort of verry simple file monitor tool.
« on: August 19, 2023, 04:26:47 pm »
Hi I'm thinking of making a tool that monitors log files. At first, all I need to know is if the log files are changee. Later I also want to look in the log files and search for certain words such as error. Before I start something new, someone knows something like this. Examples and ideas are welcome.

Edit… it is for Windows only.
« Last Edit: August 19, 2023, 04:41:01 pm by Hansvb »

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: Some sort of verry simple file monitor tool.
« Reply #2 on: August 19, 2023, 04:39:05 pm »
If it's for log files on Linux I just use the tail command in a bash script.
Very simple. I won't even bother writing something in FPC for this.

Code: Bash  [Select][+][-]
  1. #!/bin/bash
  2.  
  3. tail -F -n+1 \
  4.     /var/log/logfile1.log \
  5.     /var/log/logfile2.log \
  6. | while read line ; do
  7.  
  8.     if [[ "$line" == *"text_i_want_to_monitor"* ]]; then
  9.  
  10.         # do something
  11.  
  12.     fi
  13. done

And I have that running in a systemd service (enabled with systemctl etc).

Do note... that if you are going to build something and you have the file open, you need to make sure you can handle a log rotation (where the files are renamed). If you have your file open, it will keep reading the old file, not the new. Above in my script this is handled by the -F option. It will 'follow' the name of the file (not file-handle) and retry on failure.

Hansvb

  • Hero Member
  • *****
  • Posts: 886
Re: Some sort of verry simple file monitor tool.
« Reply #3 on: August 19, 2023, 04:40:33 pm »
I wil change the qustion. It is for Windows only.

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: Some sort of verry simple file monitor tool.
« Reply #4 on: August 19, 2023, 04:41:23 pm »
I wil change the qustion. It is for Windows only.
Ha, Ok. Then DirectoryWatcher is a nice solution.

 

TinyPortal © 2005-2018