Search code examples
rrscriptwindows-task-scheduler

R code runs well on one computer but not on another (via Task Scheduler in RScript.exe)


My issue is: when I run the following code from one laptop in RScript.exe via Task Scheduler, I get the desired output; that is the email is sent. But when I run the same code on another machine in RScript.exe via Task Scheduler, it doesn't run. Another machine (machine 2) is able to send emails (when only the code for email is run), so I think the issue is with the following part.
results <- get_everything(query = q, page = 1, page_size = 2, language = "en", sort_by = "popularity", from = Yest, to = Today)

I am unable to find what is the issue here. Can someone please help me with this?

My code is:

library(readxl)
library(float)
library(tibble)
library(string)
library(data.table)
library(gt)
library(tidyquant)
library(condformat)
library(xtable)
library(plyr)
library(dplyr)
library(newsanchor)
library(blastula)

Today <- Sys.Date()
Yest <- Sys.Date()-1

results <- get_everything(query  = "Inflation", page = 1, page_size = 2, language = 
          "en", sort_by = "popularity", from = Yest, to = Today, api_key = 
           Sys.getenv("NEWS_API_KEY"))
                      
OP <- results$results_df

OP <- OP[-c(1, 5:9)]
colnames(OP) <- c("News Title", "Description", "URL")

W <- print(xtable(OP), type="html", print.results=FALSE, align = "l")

email1 <-
  compose_email(
    body = md(
    c("<tr>", "<td>", "<table>", "<tr>", "<td>", "<b>", "Losers News", "</b>", W, 
      "</td>", "</tr>", "</table>","</td>", "<td>")    
     )
    )

 email1 %>%
 smtp_send(
 from = "[email protected]",
 to = "[email protected]",
 subject = "Hello",
 credentials = creds_key(
  "XYZ"
 )
)


                      

Solution

  • Whenever you schedule jobs, consider using a command line shell such as PowerShell or Bash to handle the automation steps, capture, and log errors and messages. Rscript fails on the second machine for some unknown reason which you cannot determine since you do not receive any error messages from console using TaskScheduler.

    Therefore, consider PowerShell to run all needed Rscript.exe calls and other commands and capture all errors to date-stamped log file. Below script redirects all console output to a .log file with messages. When Rscript command fails, the log will dump error or any console output (i.e., head, tail) below it. Regularly check logs after scheduled jobs.

    PowerShell script (save as .ps1 file)

    cd "C:\path\to\scripts"
    
    & {
        echo "`nAutomation Start: $(Get-Date -format 'u')"
    
        echo "`nSTEP 1: myscript.R - $(Get-Date -format 'u')"
        Rscript myscript.R
    
        # ... ADD ANY OTHER COMMANDS ...
        
    
        echo "`nCAutomation End: $(Get-Date -format 'u')"
    
    } 3>&1 2>&1 > "C:\path\to\logs\automation_run_$(Get-Date -format 'yyyyMMdd').log"
    

    Command Line (to be used in Task Scheduler)

    Powershell.exe -executionpolicy remotesigned -File myscheduler.ps1
    

    Note: Either change directory in TaskScheduler job settings where myscheduler.ps1 resides or run absolute path in -File argument.