4

I Have a Rscript file (Main_Script.R) which runs as a sheduled job in windows Task Scheduler every 30 Mins.Inside the Main_Script.R - i have around 13 scripts that runs every 30 mins.

I wanted to send Email from R - whenever an iteration is failed or haulted. I am using sendMailR package - and i have seen an post in SO how to send email with attachment from R in windows - on how to send emqil from R Windows.

But i am not sure about - how to send an email automatically with the error message - when the scheduled task iteration is failed or haulted.

My Main_Script.R - has source of all 13 codes.

source(paste(rootAddress,"Scripts/Part1.R",sep =''))
source(paste(rootAddress,"Scripts/Part2.R",sep =''))
:
:
:
:
source(paste(rootAddress,"Scripts/Part13.R",sep =''))

My Sheduled task looks like below with log file

"D:\xxx\R-3.0.2\bin\x64\Rscript.exe" "D:\xx\Batch_Processing\Batch_Processing_Run\Scripts\Main_Test.R" >> "D:\XXX\Batch_Processing\Batch_Processing_Run\error.txt" 2>&1

Update:

When the script encounters error - it should trigger email - with the erorr meassge and the script name or number -to denote which of the 13 scripts failed and send to a mail ID.

Prasanna Nandakumar
  • 4,295
  • 34
  • 63
  • 1
    Wrap your statements in `tryCatch` and after it's been evaluated, catch any errors and send an email. – Roman Luštrik Aug 26 '14 at 06:29
  • I have around 13 scripts - `trycatch` them would be a lot to catch each and every error , is there any other way to get the error and attach them in mail.`(options(error=(...))` saw this suggestion in internet , but not sure how to do it – Prasanna Nandakumar Aug 27 '14 at 01:52
  • You only need ONE `tryCatch` that encloses all your `source` calls. – Spacedman Aug 27 '14 at 07:15
  • So is this now two questions? Do you know how to send *any* email from R? – Spacedman Aug 29 '14 at 07:45
  • @Spacedman I am looking at some of the SO questions to sendemail, i am learning it for the first time – Prasanna Nandakumar Aug 29 '14 at 08:19
  • So you *dont* yet know how to send an email? That's not the question you're asking here, so does this question have a point any more? – Spacedman Aug 29 '14 at 09:08
  • 1
    My Question is - i dont know how to send email when script fails in batch job.For to send any email - there are example in SO - that i can refer and get to work , is what i meant – Prasanna Nandakumar Aug 29 '14 at 09:25

1 Answers1

8

Here's a solution that wraps your script of sources:

tryCatch({
source("fail1.R")
source("fail2.R")
source("fail3.R")
},
         error=function(e){cat("send email with error ",e$message,"\n")})

My scripts are:

if(x==1){stop("Fail One!")}

and similar. Hence:

> x=22
> source("doall.R")
> x=2
> source("doall.R")
send email with error  Fail Two! 

So replace my cat with your email sending and job done. The error is passed as an argument to the handler so you can get the message from it.

Here's how to do it with 13 scripts numbered as your example and identify which one went wrong:

for(i in 1:13){
 try( {
      source(paste(rootAddress,"Scripts/Part",i,".R",sep =''))
      },
      error = function(e){mailMe(i, e$message)}
    )
}

Now you just need to write the mailMe function, which gets the number of the script and the error message. It might be something like this:

mailMe = function(i, message){
  subject=paste("Error in script ",i)
  body = paste("Error was ",message," in script ",i)
  someSendmailRfunction(to="me@my.come", subject=subject,body=body, etc=etc)
}

Note you can test the mailMe function separately until it works.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Ok, i am trying your method - but i am facing issue in `sending email` as a error function.I have updated the question - that i also needed to know which of the 13 scripts failed – Prasanna Nandakumar Aug 28 '14 at 05:27
  • @PrasannaNandakumar first learn how to catch 1001 bit from `tryCatch`. the rest should fall into their place seamlessly. I would ask another question on how to tell which script produces error using `tryCatch`. Produce a reproducible example, show some effort and answer is guaranteed. – Roman Luštrik Aug 28 '14 at 06:31
  • @RomanLuštrik I am following the above answer to try to use the error function for sending email. However i havent used sendmailR package before, so i am trying my best to create the mail function - within the error module(using sendmailR package) to send mail. – Prasanna Nandakumar Aug 29 '14 at 06:38