R
Base R only (readLines on the URL). On an error it sends the message; set tb_log to the script's log file (e.g. Rscript job.R > job.log 2>&1) and its last lines go with the failure too (shortened, as they travel in the address).
The code
This is the code the Code tab generates (with start, errors and rows ticked). In the app it already contains your job's own address.
# --- 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))))How it works
The code pings /start when the script begins, the plain address with the row count when it ends well, and /fail when it fails, with the error message and, if tb_log is set, the last lines of the log.
Untick options on the Code tab to leave parts out; untick Send output lines to send no output, only the error or exit code. See what can be reported.
Run an R script on a schedule
Scheduled R scripts run with Rscript, which comes with R. On Windows give the full path, because Task Scheduler doesn't know your PATH:
schtasks /Create /TN "CRM import" /SC DAILY /ST 05:30 /TR "\"C:\Program Files\R\R-4.4.1\bin\Rscript.exe\" \"C:\jobs\crm_import.R\""
On Linux or a Mac, one crontab line (crontab -e):
30 5 * * * /usr/bin/Rscript /home/me/jobs/crm_import.R
Check the R version folder: after an R update the old path no longer exists and the task fails before your script even starts. TaskBeacon then shows the job as Missed.
Why the exit code isn't enough
An error that stops the script makes Rscript exit with a non-zero code, so the scheduler can see it. It does not see the quiet failures: a query that returns no rows, a file that wasn't updated, a tryCatch that swallows the error and carries on, or a warning that turned the result into NA.
The code above sends /fail with the error message (and the last lines of tb_log, if set), then calls stop(e) again so the exit code stays honest.
Report the row count
The last line sends nrow(df). Replace df with the data frame your script produces. To send more numbers, add them to the address with tb_num(), which writes 100000 as 100000, not 1e+05:
tb_ping(paste0("?rows=", tb_num(nrow(df)), "&total=", tb_num(sum(df$amount))))
Common problems
- Packages not found. Task Scheduler may run as another user with a different library. Install packages for all users, or set
.libPaths()at the top of the script. - Working directory. Relative paths are relative to the task's Start in folder (cron: your home). Use full paths or
setwd(). - No ping behind a proxy.
readLines()on a URL uses R's download method; if your company needs a proxy, sethttp_proxy/https_proxyfor the task. - Network drives. A task that runs while you are logged out doesn't see mapped drive letters; use UNC paths.
Use cases: Know when a scheduled R script fails. More questions: FAQ.