Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

E-mail (or similar) notification when code execution is finished

I am currently doing several simulations in R that each take quite a long time to execute and the time it takes for each to finish varies from case to case. To use the time in between more efficiently, I wondered if it would be possible to set up something (like a e-mail notification system or similar) that would notify me as soon a a chunk of simulation is completed.

Does somebody here have any experience with setting up something similar or does someone know a resource that could teach me to implement a notification system via R?

like image 423
Fred Avatar asked Jun 22 '19 16:06

Fred


People also ask

How do I send an email notification in ServiceNow?

Login into your ServiceNow tenant & in the search bar type “notifications”. Now click on the “Notifications” option under the System Notification. Then click on the “New” button, to create the new notification.


2 Answers

I recently saw an R package for this kind of thing: pushoverr. However didn't use it myself - so not tested how it works. But seems like it might be useful in your case.

like image 176
Karolis Koncevičius Avatar answered Nov 03 '22 01:11

Karolis Koncevičius


I assume you run the time consuming simulations on a server, correct? If these run own you own PC, your PC will be slow as hell anyway and I would not see something beneficial in sending a mail to myself.

For long calculations: Run them on a virtual machine, I use the following workflow for my own calculations.

  1. Write your R script. Important: Write a .txt file when the calculation file in the end. The shell script will search in a loop for the file to exist.
  2. Copy that code an save it as Python script. I tried one day to get MailR running a Linux and it did not work. This code worked on the first try.
#!/usr/bin/env python3
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
email_user = '[email protected]'
email_password = 'password'
email_send = 'theothersmail.com'
subject = 'yourreport'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Calculation is done'
msg.attach(MIMEText(body,'plain'))
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send,text)
server.quit()

Make sure you are allowed to run the script.

  1. sudo chmod 777 /path/script.R sudo chmod 777 /path/script.py

  2. Run both your script.R and script.py inside a script.sh file. It looks the the following:

R < /path/script.R --no-save  
while [ ! -f /tmp/finished.txt ]
do
  sleep 2
done
python path/script.py

This may sound a bit overwhelming if you are not familiar with these technologies, but think this is a pretty much automated workflow, which relieves your own resources and can be used "in production". (I use this workflow to send me my own stock reports).

like image 41
DSGym Avatar answered Nov 03 '22 00:11

DSGym