Shell script to monitor if new file is not receive on folder it will give you mail alert

 In this post we will get an email alert if the monitor folder have not receiving any files.

below there is a shell script, if you wan to know more detail on send mail concept you could refer here

--------------Script Start------------------

# cat dir_moni_v2.sh

#!/bin/bash

TO="email_id@email.com"

#MESSAGE=$'Hi There,\nI think today there is no file arrived yet.\n\nThanks\nUnix Support'

PAT=/home/Akil/moni_dir

echo -e "

Hi There,

Date `date`

This is to inform that there is no file received today on this path $PAT

Thanks

Unix Support Team

Automated email

" >/tmp/message.txt


Value=`find $PAT -type f -daystart -ctime 0 -print | wc -l`


if [ $Value -gt 0 ]; then

        echo "File received today"

else

        echo "File not received today so far so sending mail to the recipient"

        tr -d '\015' < /tmp/message.txt | mail -v -s "Directory Monitoring Alert" $TO

        #echo $MESSAGE | mail -v -s "Directory Monitoring Alert" $TO

        #echo "<Messages>" | mail -s "<Subject of mail>" <Mail_ID>

fi

----------------------Script End---------------------------

output

Scenario 1:

[root@centos ~]# sh dir_moni_v2.sh

File received today

Scenario 2:
[root@centos ~]# sh dir_moni_v2.sh
File not received today so far so sending mail to the recipient
Resolving host smtp.aaaa.com . . .
. . . message is sent.

email :

Hi There,

Date Wed Oct 13 02:23:36 PDT 2021

This is to inform that there is no file received today on this path $PAT

Thanks

Unix Support Team

Automated email




Note : you can set this script under cron task and make sure you respective time to check the folder.



Post a Comment

0 Comments