share
Unix & LinuxAutomatic email setup in unix server by script after checking particular logs
[0] [1] Maha
[2020-04-17 07:23:03]
[ shell-script ]
[ https://unix.stackexchange.com/questions/580622/automatic-email-setup-in-unix-server-by-script-after-checking-particular-logs ]

I want to send an automatic email from Unix server after checking particular log is running or not. Every three hours, i want to login in and check X.log and y.log ran or not and taking manaully when it ran. Can anyone help to automate it ?

you should check about cron service to run every three hours... and more details needed for "ran" meant for.... - Siva
(2) Which bit are you stuck on? The sending an email? Scheduling a task to run every three hours? Checking the log files for particular patterns or sizes? - Chris Davies
[0] [2020-04-17 10:02:35] Santosh Garole

Hey You can use below script to check if file exists & set it in cronjob to run it every three hours.

CODE:

#!/bin/bash
export LOGNAME="x.log"
export DATE=$(date "+%Y-%m-%d-%T")
export MAILID="Emailid"


if [[ -e "$LOGNAME" ]];then
        mail -s "$HOSTNAME: is having $LOGNAME on @ $DATE" $MAILID < "$LOGNAME"
else
        mail -s "$HOSTNAME: is not having $LOGNAME on @ $DATE" $MAILID 
fi

Setting up in crontab:

#crontab -e

Add the below lines:

* */3 * * *   <path of your script to run>

If this works for you @Maha Can you please accept the answer. For accepting it please follow: meta.stackexchange.com/questions/5234/ - Santosh Garole
Your use of $LOGNAME overrides a POSIX reserved variable. Generally it's better to use lowercase variables, i.e. $logname in this instance - Chris Davies
1