Home > Mobile >  Reading log file and if error occurs restart service
Reading log file and if error occurs restart service

Time:11-11

I'm trying to create a script where it monitors a log file and if an error appears, it sends an e-mail with python. The script works, but once it detects an error, it starts spamming emails. Is there a way to avoid this?

# cat /usr/local/bin/monitor.bash
#!/bin/bash

tail -fn0 /var/log/syslog | while read line ; do
echo "${line}" | grep -i "error" > /dev/null
if [ $? = 0 ] ; then
  python emailsend.py
fi
done

Thanks in advance!

Edit: I'm planning to have this running as a service for constant monitoring of logs. Once it detects an error, I want it to sent an e-mail once, then it will restart some services(haven't added it to the script yet) and keep monitoring the file until it rotates. I hope this narrows it down a bit.

CodePudding user response:

If I get logic of the code correctly, you can throw in some lines into the log to prevent getting into if clause and restart service.

# cat /usr/local/bin/monitor.bash
#!/bin/bash

tail -fn0 /var/log/syslog | while read line ; do
echo "${line}" | grep -i "error" > /dev/null
if [ $? = 0 ] ; then
  python emailsend.py
  # append line to the log to prevent grepping of 'error'
  echo 'dummy line' >> /var/log/syslog
  # restart your services here
fi
done

Hope it helps.

  • Related