LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-27-2023, 06:24 AM   #1
Avinash Yadav
LQ Newbie
 
Registered: Jan 2023
Posts: 4

Rep: Reputation: 0
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
~
 
Old 01-27-2023, 06:30 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
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.

Last edited by TenTenths; 01-27-2023 at 06:32 AM.
 
Old 01-27-2023, 10:13 PM   #3
elgrandeperro
Member
 
Registered: Apr 2021
Posts: 344
Blog Entries: 2

Rep: Reputation: Disabled
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.
 
Old 01-28-2023, 12:47 AM   #4
Avinash Yadav
LQ Newbie
 
Registered: Jan 2023
Posts: 4

Original Poster
Rep: Reputation: 0
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
~
 
Old 01-28-2023, 03:32 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
Quote:
Originally Posted by Avinash Yadav View Post
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 View Post
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.

Last edited by TenTenths; 01-28-2023 at 03:34 AM.
 
2 members found this post helpful.
Old 01-28-2023, 06:33 AM   #6
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,075
Blog Entries: 41

Rep: Reputation: 30
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.
 
Old 01-28-2023, 06:35 AM   #7
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,075
Blog Entries: 41

Rep: Reputation: 30
Tell me if you need more hints or help!
 
1 members found this post helpful.
Old 01-28-2023, 06:36 AM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
Quote:
Originally Posted by des_a View Post
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.
 
1 members found this post helpful.
Old 01-28-2023, 09:40 AM   #9
elgrandeperro
Member
 
Registered: Apr 2021
Posts: 344
Blog Entries: 2

Rep: Reputation: Disabled
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

Last edited by elgrandeperro; 01-28-2023 at 12:49 PM.
 
1 members found this post helpful.
Old 02-02-2023, 08:55 PM   #10
Avinash Yadav
LQ Newbie
 
Registered: Jan 2023
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 02-02-2023, 09:38 PM   #11
elgrandeperro
Member
 
Registered: Apr 2021
Posts: 344
Blog Entries: 2

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rsync Script with cron and email notification opeth281 Programming 1 04-03-2020 11:56 PM
Email notification script failing to send when executed with cron pcarlso7 Linux - Software 2 10-24-2017 12:13 PM
Cron Email Notification - Perl Script jamesyreid Programming 3 07-21-2009 07:56 AM
cron email notification, status 0x0045 jovie Fedora 5 10-11-2007 09:56 AM
Cron Email Notification(?) jamesyreid Linux - General 2 12-17-2001 10:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration