LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Email notification through Cron utility (https://www.linuxquestions.org/questions/linux-newbie-8/email-notification-through-cron-utility-4175721367/)

Avinash Yadav 01-27-2023 06:24 AM

Email notification through Cron utility
 
Hi Team,

I am a new learner in Linux, I work in AWS Cloud, We have bunch of AWS Linux Servers, where we have used cron utility for patch update on all the linux servers.

Can someone please provide me the setup or the configuration on how can I receive an email notification once the update has been done.?

Below is the current setup I have and the log gets generated at /tmp/yum_updates.log, Once the upgrade is done,I would like to receive an email notification.

How this can be achieved.

========================================================
Every Tuesday at 02:30 AM GMT patches get installed.

Ansible: yum patching autoupdate
30 2 * * 2 root (set -x; date; yum -y --disablerepo=* --enablerepo=rhel-7-server-rhui-rh-common-rpms --enablerepo=rhel-7-server-rhui-rpms --enablerepo=rhui-client-config-server-7 --exclude=apigee* --exclude=edge* update; date) &> /tmp/yum_updates.log
~

TenTenths 01-27-2023 06:30 AM

Change your cron job to a script that does the yum update and then either cat the resulting log file in to sendmail where you'll get the log as the body of the message or install and use mutt which makes it easier to send the log file as an attachment.

Just remember that AWS EC2 instances have mail levels throttled to a small amount of e-mails per instance per day, unless you use SES SMTP as a relay.

elgrandeperro 01-27-2023 10:13 PM

https://docs.aws.amazon.com/ses/latest/dg/postfix.html

That is one way.

If you have a inbound email relay, you can allow it (via the access table) and just forward your email using postfix and relay configuration.
Since this mail is bound for your mail domain, it can be accepted without too much checking.

If you have a email service, you might have to adhere to their rules that prevent spam. Things like STARTTLS, spf, dkim, dmarc. The days of spinning up an autonomous mail server running on port 25 that delivers everywhere are over.

Avinash Yadav 01-28-2023 12:47 AM

Hey Guys, Appreciate your response

1 ) I find it difficult to implement this. Would you mind providing me a sample script for the update with the email notification ?

2) Would it be possible to edit the file which I already have with email notification ? If Yes, where and how to implement?

Ansible: yum patching autoupdate
30 2 * * 2 root (set -x; date; yum -y --disablerepo=* --enablerepo=rhel-7-server-rhui-rh-common-rpms --enablerepo=rhel-7-server-rhui-rpms --enablerepo=rhui-client-config-server-7 --exclude=apigee* --exclude=edge* update; date) &> /tmp/yum_updates.log
~

TenTenths 01-28-2023 03:32 AM

Quote:

Originally Posted by Avinash Yadav (Post 6407435)
1 ) I find it difficult to implement this. Would you mind providing me a sample script for the update with the email notification ?

Sure, my consultancy rates are $200/hr.

Try it yourself and post what you've tried and where the problems are.

I'm happy to help, but you need to put some learning effort into this too.


Quote:

Originally Posted by Avinash Yadav (Post 6407435)
2) Would it be possible to edit the file which I already have with email notification ? If Yes, where and how to implement?

Sure, you could just append the mail part to your existing one line cron entry using ; to delimit commands. However, a three line script is much easier to maintain.

des_a 01-28-2023 06:33 AM

Hint:

1. The sendmail, and postfix commands can be used to send mail from the command line.
2. The cron job will operate on the command line, usually.
3. Look at "man 5 crontab".

As an added bonus, something I used to do, is send wol commands to computers, which you can find the wol program somewhere out there. It can be used to make a schedule for servers, so they do not have to run 24/7.

des_a 01-28-2023 06:35 AM

Tell me if you need more hints or help!

TenTenths 01-28-2023 06:36 AM

Quote:

Originally Posted by des_a (Post 6407476)
As an added bonus, something I used to do, is send wol commands to computers, which you can find the wol program somewhere out there. It can be used to make a schedule for servers, so they do not have to run 24/7.

Less relevant to the OP as they mention they are using AWS, so I'm assuming EC2 instances that can be scheduled to start / terminate if required.

elgrandeperro 01-28-2023 09:40 AM

You need to setup the postfix relay first, or it won't work. If you've done that and it works it is simple.

Getting the mail in, as I said, is the easy part. Here is a cron I just installed in cron. (I prefer to put the script name call in cron so you don't have to funny crontab problems and can run it to test easier):

#!/bin/sh

DATE=`/bin/date`

echo "TEST POST mailinglist on listserver on $DATE" | mailx -v -s"mailinglist post $DATE" test-list@listserver.example.com

mutt is similar. like mutt -s "Test Email" john@mydomain.com

Avinash Yadav 02-02-2023 08:55 PM

I did tried to send an email to my ID, The logs says the message is sent OK, but unfortunately I did not receive any email. I checked my spam folder as well. What could be the issue ?

Feb 3 02:50:08 ip-10-238-194-57 postfix/cleanup[26089]: B889AC57180: message-id=<20230203025008.B889AC57180@ip-10-238-194-57.eu-west-1.compute.internal>
Feb 3 02:50:08 ip-10-238-194-57 postfix/qmgr[1339]: B889AC57180: from=<root@ip-10-238-194-57.eu-west-1.compute.internal>, size=533, nrcpt=1 (queue active)
Feb 3 02:50:15 ip-10-238-194-57 postfix/smtp[26092]: B889AC57180: to=<avinash.yadav@netenrich.com>, relay=mx2-us1.ppe-hosted.com[67.231.154.163]:25, delay=6.8, delays=0.04/0.04/6.5/0.23, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 64986440061)
Feb 3 02:50:15 ip-10-238-194-57 postfix/qmgr[1339]: B889AC57180: removed

====================================

Also I edited the script as below what was suggested in this thread. Sorry my linux skills are just too bad.


#Ansible: yum patching autoupdate
30 2 * * 2 root (set -x; date; yum -y --disablerepo=* --enablerepo=rhel-7-server-rhui-rh-common-rpms --enablerepo=rhel-7-server-rhui-rpms --enablerepo=rhui-client-config-server-7 --exclude=apigee* --exclude=edge* update; date) &> /tmp/yum_updates.log
DATE=`/bin/date`

echo "Patch autoupdate 33bcd on $DATE" | mailx -v -s"mailinglist post $DATE" avinash.yadav@netenrich.com

elgrandeperro 02-02-2023 09:38 PM

There is a thing called spf records. Your mail relay might need to have this updated in DNS. These are the ips in which mail is expected to originate, which is now your AWS instance. I'd check whoever runs your email, you might have bounced but it might not deliver back to you.


All times are GMT -5. The time now is 08:13 PM.