Search a pattern in a log file

I have file which gets updated every minute/second. Is it possible in shell scripting that I can search for some pattern infinitely in this file and if it finds that pattern, alert the user.

A sample of log file is below. The following file is getting updated every second. I want to alert the admin if it finds a "logout" each time. In simple words a kind of monitoring.

1234:login
234566:login
123456:logout

Something like this (untested, run with nohup in background):

tail -f <your_file> |
  while IFS= read -r; do
    case $REPLY in
      ( *your pattern* ) run_your_commands ;;
    esac
  done    
1 Like

You can try this:

nohup tail -f file | awk '/logout/{system("echo message | write root")}' &

It will run in the background until someone kills it or reboots the server (it will wun after you log out from console session too).

1 Like