User:MPopov (WMF)/Notes/Email notifications

If I have a script running in a screen on a stat host that takes a long time to run, I find it useful to have it notify me by email upon completion. Fortunately, it's very easy to do with the SMTP service on all the analytics clients and the mailx command line utility.

Python

edit
import os

message_body = "Finished"
subject = "Long process"
to_address = "mpopov+notifications@wikimedia.org"
from_address = "`whoami`@`hostname` <mpopov@wikimedia.org>"
# ^ e.g. 'bearloga@stat1008 <mpopov@wikimedia.org>'

shell_command = f'echo "{message_body}" | mailx -r "{from_address}" -s "{subject}" {to_address}'

os.system(shell_command)

This relies on glue R package for interpreted string literals with {} placeholders.

library(glue)

# ...run some long process...
# ...save/cache the output...

message_body <- "Finished"
subject <- "Long process"
to_address <- "mpopov+notifications@wikimedia.org"
from_address <- "`whoami`@`hostname` <mpopov@wikimedia.org>"
# ^ e.g. 'bearloga@stat1008 <mpopov@wikimedia.org>'

shell_command <- glue('echo "{message_body}" | mailx -r "{from_address}" -s "{subject}" {to_address}')
system(shell_command)

Replace to_address and from_address with your email address.

With timing information

edit

This relies on tictoc R package for timing execution of commands.

library(glue)
library(tictoc)

tic("Model-fitting script")
  tic("Data read & refinement")
  # ...data import & wrangling code...
  toc(log = TRUE)
  tic("Model 1 fit")
  # ...fit one model...
  toc(log = TRUE)
  tic("Model 2 fit")
  # ...fit an alternative model...
  toc(log = TRUE)
  # ...save fits for loading later...
toc(log = TRUE)

tic_log <- tic.log(format = TRUE)
message_body <- paste0(tic_log, collapse = "\n")

subject <- "Model fitting done"
to_address <- "mpopov+notifications@wikimedia.org"

from_address <- "`whoami`@`hostname` <mpopov@wikimedia.org>"
# e.g. 'bearloga@stat1008 <mpopov@wikimedia.org>'

shell_command <- glue('echo "{message_body}" | mailx -r "{from_address}" -s "{subject}" {to_address}')
system(shell_command)

message_body would look like:

Data read & refinement: 0.001 sec elapsed
Model 1 fit: 0.001 sec elapsed
Model 2 fit: 0.001 sec elapsed
Model-fitting script: 0.086 sec elapsed