Know when a scheduled R script fails
You scheduled an R script with taskscheduleR, Windows Task Scheduler or cron, and one morning the dashboard shows last week's numbers. The script had been failing for days and nothing told you. The error was written down, just not anywhere you look; a tryCatch can send it to you the moment it happens.
Where the error goes
R itself does report failures. When a script run with Rscript stops on an error, R ends with exit status 1 (the R documentation for quit describes this for non-interactive use). The problem is that nobody is listening:
- taskscheduleR (version 1.8 on CRAN, 2023) creates a Windows task that runs
Rscriptand sends its output to a.logfile next to the script. The error is in that log, and Task Scheduler shows0x1, but the package has no notification of its own. - Task Scheduler keeps the result in the Last Run Result column, which nobody opens unless something already looks wrong.
- cron (also via the cronR package) mails a job's output to the crontab's owner, but only if the machine can send mail; without a mail program the output goes to the system log.
Two common failures don't even reach R's error handling: the task can't find Rscript.exe or the working folder (relative paths, mapped drives that don't exist when nobody is logged on), or the task doesn't start at all because the PC was off or asleep. And a script that runs without error but reads an empty file exits with 0.
Catch the error in the script
The code below uses base R only. It pings TaskBeacon at the start, wraps your code in tryCatch, and on an error sends the message (and, if you set tb_log, the last lines of the log) to /fail, then stops with the same error, so the exit status stays 1. When the script ends well it sends the row count of df.
# --- TaskBeacon: top of the script (base R only) ---
tb <- "https://taskbeacon.io/ping/your-job-id"
tb_log <- "" # optional: this script's log file (e.g. Rscript job.R > job.log 2>&1); its last 20 lines go with a failure
tb_ping <- function(path = "") {
old <- options(timeout = 10) # wait at most 10 seconds (R's default is 60)
on.exit(options(old))
invisible(try(suppressWarnings(readLines(paste0(tb, path), warn = FALSE)), silent = TRUE))
}
tb_num <- function(x) utils::URLencode(format(x, scientific = FALSE, digits = 15, trim = TRUE), reserved = TRUE)
tb_tail <- function() {
if (!nzchar(tb_log) || !file.exists(tb_log)) return("")
text <- paste(utils::tail(readLines(tb_log, warn = FALSE), 20), collapse = "\n")
if (nchar(text) > 700) text <- sub("^[^\n]*\n", "", substr(text, nchar(text) - 700, nchar(text)))
paste0("&log=", utils::URLencode(text, reserved = TRUE))
}
tb_ping("/start")
tryCatch({
# --- your code here ---
df <- data.frame(x = 1:3) # example data: replace with your own
}, error = function(e) {
tb_ping(paste0("/fail?error=", utils::URLencode(substr(conditionMessage(e), 1, 200), reserved = TRUE), tb_tail()))
stop(e)
})
# --- TaskBeacon: end of the script ---
tb_ping(paste0("?", "rows=", tb_num(nrow(df))))In the app, the Code tab of your job gives you this code with the job's own address instead of your-job-id, and you can untick the parts you don't need.
Replace the example line with your code and make sure the result you care about ends up in df, or change nrow(df) to whatever you want to count. With taskscheduleR, set tb_log to the log file it writes (the script's name with .log), so the alert shows R's own output.
What you get
- Failed right away, with the error message, for example
cannot open file 'S:/data/sales.csv': No such file or directory. - Missed when the script didn't report by its planned time plus grace. This catches the runs where R never started.
- Late when the start ping came and the run is taking much longer than usual.
- Odd data when the row count drops more than 40% below the recent runs or is 0. See 0 rows or far fewer than usual.
The R page in the Docs shows how to schedule Rscript with schtasks or a crontab line, and the common problems with paths and packages under a scheduler.
Without TaskBeacon
Wrap the script in tryCatch yourself and send an email from the error handler, for example with the blastula or emayili package and your SMTP server. That reports errors inside R. For runs that never start, check the log file's date every morning, or have another job raise an alarm when the log is older than a day. Both work; the second is easy to forget once it is set up on one PC. A heartbeat check does that part from outside the machine.
Connected Claude or ChatGPT to TaskBeacon? Ask it "what failed last night, and why?" and it answers from your jobs. How that works.
Related
In the Docs: R · Schedules · What can be reported.
- Get an alert when a Python cron job or scheduled script failscron reports output, not success or failure; Task Scheduler shows 0x1 to nobody.
- Get an alert when a job loads 0 rows, or far fewer than usualExit code 0 says nothing about how many rows arrived.
- Find out when a scheduled job didn't run at allA run that never happened leaves no error to alert on.
TaskBeacon is free to start, with every feature open for now and no credit card. Create a job, paste the code, and the first ping shows up on the job page.
Start free View live demoSources
- R documentation: quit (non-interactive errors end R with status 1) (R 4.6 docs, undated; checked 2026-11-09)
- CRAN: taskscheduleR 1.8 (schedules R scripts with schtasks, writes a .log file) (2023-07-02, still the current version; checked 2026-11-09)
- taskscheduleR README: the log is in the same folder as the R script (undated; checked 2026-11-09)
- Posit Forum: how to schedule an R script using cronR or other methods (2023-07-25, old: a sign of demand)
- Microsoft Q&A: task shows as completed successfully, but nothing happened (R script) (2021-06-16, old: a sign of demand)
Page checked against its sources on 2026-11-09. Menus and limits of other products change; if something here no longer matches, write to support@taskbeacon.io.