Check File for Contents then Send Email (without mailtools)

Random

A simple BASH script that you can run as a CRON job to check if a particular string is present in a file, and if not it sends you an email to say the file no longer has a particular string. And the email send just uses sendmail (assuming that is installed and configured).

You can manually run the script if you wish, say if you called it “checkstring.sh”, with: ./checkstring.sh

#!/bin/bash

if grep -q 'mystring' "/root/myfile.txt";
then
  echo -n "String:"
  echo -e "\e[31m Present\e[0m"
else
  echo -n "String:"
  echo -e "\e[32m No longer present!\e[0m"
  /usr/lib/sendmail bob@domain.com < /root/message.txt
fi

The content of the message.txt file is something like:

From: sender@domain.com
To: bob@domain.com
Subject: String is no longer present!
Hello!

It looks like the string has gone!

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *