How to send mail at specific calculated time interval?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to send mail at specific calculated time interval?
# 1  
Old 06-05-2017
How to send mail at specific calculated time interval?

Hi All,
I want to send an email if the time difference from previous mail sent is more than or equal to 30 mins.
I have written below code and it's working fine.
In this script I am storing previous mail sent time in txt file.
Instead of storing in txt file how to store in a variable.
Initially I am putting some date manually in the file prev_mail_sent.txt to assume initial mail sent.

Please help to implement this without creating any file use variable.
Code:
v_par=8
num_minutes=30
if [[ $v_par -eq 8 ]]; then
mail_date=`cat prev_mail_sent.txt`
curr_date=$(date +"%Y-%m-%d %H:%M:%S")
diff_sec=`echo "$(($(date -d "$curr_date" '+%s') - $(date -d "$mail_date" '+%s')))"`
d_min=`expr $diff_sec / 60`
if [[ $d_min -ge  $num_minutes ]]; then
mail -s "mail sent"
rm -f prev_mail_sent.txt
printf "$(date +"%Y-%m-%d %H:%M:%S")\n" > prev_mail_sent.txt
fi
fi

Thanks in advance.
# 2  
Old 06-05-2017
You have quite a bit in there that is unnecessary. If you keep all date/times as seconds, it will make it a little easier:-
Code:
v_par=8
num_minutes=30

if [[ $v_par -eq 8 ]]; then
   read mail_date < prev_mail_sent.txt
   curr_date=$(date +%s)

   ((diff_sec=$curr_date - $mail_date) ))
   ((diff_min=$diff_sec / 60))

   if [[ $diff_min -ge  $num_minutes ]]; then
      mail -s "mail sent"                              # You probably should specify a target address and some content here
      rm -f prev_mail_sent.txt
      printf "$curr_date\n" > prev_mail_sent.txt
   fi

fi


Does that help a little? I've trimmed out unnecessary sub-processes cause by back-ticks `, expr and the cat too. it might be marginal, but they (along with getting the current date once) should keep this a little more consistent.



Let us know if this suits your needs.

Kind regards,
Robin
# 3  
Old 06-05-2017
Hi Robin,

Thanks for making the code simple.

My main concern is we are storing the previous email sent in a prev_mail_sent.txt file. Instead of that we have to store in some variable.
Could you please help me with that.

Thanks in advance.

Last edited by rbatte1; 06-05-2017 at 07:12 AM.. Reason: Emboldened file name and corrected spelling
# 4  
Old 06-05-2017
Is your script going to loop forever? (presumably pausing so you don't murder the CPU)

If so, then you should simply assign the value of $curr_date to mail_date when you send a mail.

Rather than forgetting about it altogether, I would still write the file and move the line read mail_date < prev_mail_sent.txt to before the loop so that you read in a sensible starting value and if you script terminates for some reason (intentionally or otherwise) then when you start it up again, it will know where to pick up from.

An alternate to a loop might be to schedule the script every minute as a run-through-once process that stores the value in a file. If the script as a whole might take quite a while to run (depending on what other processing you are doing within it) then you might need to ensure you don't run it twice at the same time. There are various mechanisms for doing that if you need them. Let us know if you want help with these and I'm sure someone can work with you.



Robin
# 5  
Old 06-05-2017
Quote:
Originally Posted by nalu
My main concern is we are storing the previous email sent in a prev_mail_sent.txt file. Instead of that we have to store in some variable.
This is quite simple: at some point you have to create said text file:

Code:
some_process > /text/file

i.e.:

Code:
printf "%s\n" "This is the mail text." > /text/file

Now, instead of doing that, you put that output into a variable:

Code:
variable="$(some_process)"

When you change your script test what you have written first this way:


Code:
[...your script here...]

variable="$(some_process)"
echo 'mail-text =='$variable'=='

[...your script continued here...]

I hope this helps.

bakunin
# 6  
Old 06-05-2017
Hi All,

I have tried the below code it's not working.It's sending mail every time when I have executed.

I have hard coded last_mail_date1 for initial comparison.After that once mail sent after 10 mins it has to update with current date.
Code:
v_par=8
num_minutes=10
if [[ $v_par -eq 8 ]]
then
last_mail_date1="2017-06-05 01:47:25"
last_mail_date=$last_mail_date1
curr_date=$(date +"%Y-%m-%d %H:%M:%S")
diff_sec=`echo "$(($(date -d "$curr_date" '+%s') - $(date -d "$last_mail_date" '+%s')))"`
diff_min=`expr $diff_sec / 60`
if [[ $diff_min -ge  $num_minutes ]]; then
mail -s "mail sent"
last_mail_date1=$(date +"%Y-%m-%d %H:%M:%S")
fi
fi

Please help me.

Thanks in advance.
# 7  
Old 06-05-2017
Can you run this with the following at/near the top and show us the output please?
Code:
set -x

It will better show us the script flow.


Kind regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Client was not authenticated to send anonymous mail during MAIL FROM (in reply to MAIL FROM comm

I am having trouble getting mail to work on a red hat server. At first I was getting this message. Diagnostic-Code: X-Postfix; delivery temporarily suspended: connect to :25: Connection refused Then added the port to my firewall. Then I temporarily turned off selinux. I then copied this file... (1 Reply)
Discussion started by: cokedude
1 Replies

2. Red Hat

Script taking more time to send report to mail

Hi, I schedule a script on linux server and it is taking more time say "30 minutes" to send the report via mail. Could you please suggest a way to speed up sending report to mail? OS version -- Red Hat Enterprise Linux Server release 6.5 (Santiago) Regards, Maddy (2 Replies)
Discussion started by: Maddy123
2 Replies

3. Shell Programming and Scripting

Script that gathers specific values from files and puts it into HTML to send e-mail

Hi All, Sorry for long topic here. So the drill goes like that, I need a script which gathers different values from different files/locations. A_CT=`cat a.dat | awk -F'|' '{print $1}' >> report.txt` B_CT=`cat b.dat | awk -F'|' '{print $3}' >> report.txt` C_CT=`cat c.dat | awk -F'|'... (4 Replies)
Discussion started by: shivakid
4 Replies

4. Shell Programming and Scripting

Main script triggers second and it has to run at specific interval

Hi Friends, I am newbie to shell programming and I am stuck trying to accomplish following task.We use Bamboo CI which executes script1 passing parameters. This Main script executes script2 as backend process as part of one of it statements. Task of script2 is to essentially check whether a... (0 Replies)
Discussion started by: aditya206
0 Replies

5. Shell Programming and Scripting

Mailing a file at a particular time interval

Folks, I need to send a .log file to my email for every one hour from a server that too only in a particular time period. Is awk mandate here? or can we do it in shell scripting only? Please help me in sorting this out. Cheers, Arun (3 Replies)
Discussion started by: ArunJanga
3 Replies

6. UNIX for Advanced & Expert Users

need to configure mail setting to send mail to outlook mail server

i have sun machines having solaris 9 & 10 OS . Now i need to send mail from the machines to my outlook account . I have the ip adress of OUTLOOK mail server. Now what are the setting i need to do in solaris machines so that i can use mailx or sendmail. actually i am trying to automate the high... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

7. Shell Programming and Scripting

how to write a shellscript to send a mail alert to the website user based on license expiration time

hi, i am very much new to shell scripting i have a requirement that i have to develop a License Renewal Alert system that has to give a alert mail to the users before 30days of user account expiration, by checking expiration date of the user with the data base, this system will... (0 Replies)
Discussion started by: deepu_Shscripts
0 Replies

8. UNIX for Dummies Questions & Answers

Copying files between two time interval

Hi All, I am new to this forum.... Can neone please help me how to copy files between two time intervals i.e. I need to copy files from 6.30 to 9.30 on 5th June 09. Any help is appreciated. (2 Replies)
Discussion started by: Pratik4891
2 Replies

9. Shell Programming and Scripting

Send mail with attachments automatically in a interval period

hai everyone I want to send mail with attachments automatically for every 15 minutes getting different E-mail IDs from a file if any script is available , Please send to me .. This will be very useful for my project.... Thanks for your time..... Felix ... (3 Replies)
Discussion started by: Leo Felix
3 Replies

10. Solaris

Time Wait interval

What is the time_wait interval for Solaris 8/9??? and is it configurable??? For example sometimes a clients pc will freeze up dropping the connection, closing the port. The problem is on our side our system still thinks their logged in (until it realizes it dropped on the otherside and drops on... (1 Reply)
Discussion started by: eloquent99
1 Replies
Login or Register to Ask a Question