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

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 06:31:30

问题


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?


回答1:


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.




回答2:


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 = 'youownmail@gmail.com'
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).



来源:https://stackoverflow.com/questions/56717269/e-mail-or-similar-notification-when-code-execution-is-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!