Stata
Uses curl through shell: curl.exe on Windows 10 and 11, curl on macOS and Linux (the code picks one with c(os), so the same do-file runs everywhere). Stata in batch mode always exits with 0, so the error is caught inside the do-file and sent with Stata's r() code. Set tb_log to the do-file's log (stata -b do job.do writes job.log) and its last 20 lines go with the failure.
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 do-file ---
local tb "https://taskbeacon.io/ping/your-job-id"
* curl.exe is built into Windows 10 and 11; macOS and Linux have curl
local tb_curl = cond(c(os) == "Windows", "curl.exe", "curl")
local tb_log "" // optional: this do-file's log (batch mode: stata -b do job.do, or StataMP-64.exe /e do job.do on Windows, writes job.log in the current folder); its last 20 lines go with a failure
shell `tb_curl' -fsS -m 10 "`tb'/start"
capture noisily {
* --- your code here (the example data: replace with your own) ---
sysuse auto, clear
}
quietly {
local tb_rc = _rc
if `tb_rc' {
* the last 20 lines of the log (if tb_log is set) go with the failure
local tb_data ""
if `"`tb_log'"' != "" {
tempfile tb_tail
tempname tb_in tb_out
capture {
file open `tb_in' using `"`tb_log'"', read text
local tb_n 0
file read `tb_in' tb_line
while r(eof) == 0 {
local tb_n = `tb_n' + 1
local tb_k = mod(`tb_n', 20)
local tb_l`tb_k' = substr(`"`macval(tb_line)'"', 1, 300)
file read `tb_in' tb_line
}
file close `tb_in'
file open `tb_out' using `"`tb_tail'"', write text replace
forvalues tb_i = `=max(1, `tb_n' - 19)'/`tb_n' {
local tb_k = mod(`tb_i', 20)
file write `tb_out' `"`macval(tb_l`tb_k')'"' _n
}
file close `tb_out'
local tb_data `"--data-binary "@`tb_tail'" -H "Content-Type: text/plain""'
}
capture file close `tb_in'
capture file close `tb_out'
}
shell `tb_curl' -fsS -m 10 `tb_data' "`tb'/fail?rc=`tb_rc'"
exit `tb_rc'
}
}
* --- TaskBeacon: end of the do-file ---
local tb_rows = _N
shell `tb_curl' -fsS -m 10 "`tb'?rows=`tb_rows'"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 Stata's return code and, if tb_log is set, the last 20 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 a do-file on a schedule
Stata for Windows runs a do-file without a window with the /e switch (with /b it waits for you to click OK, which a schedule can't do). Use the full path of your Stata edition:
"C:\Program Files\Stata18\StataMP-64.exe" /e do "C:\jobs\sales.do"
To start it every morning, create a task from a Command Prompt (one line):
schtasks /Create /TN "Sales report" /SC DAILY /ST 06:00 /TR "\"C:\Program Files\Stata18\StataMP-64.exe\" /e do \"C:\jobs\sales.do\""
On macOS or Linux, run it in batch mode from cron (crontab -e; the program name depends on your edition, e.g. stata-mp):
0 6 * * * cd /home/me/jobs && /usr/local/stata/stata-mp -b do sales.do
The code above uses curl.exe on Windows and curl elsewhere (it checks c(os)), so the same do-file works on every machine.
Stata writes sales.log into its working folder. In Task Scheduler set Start in to the do-file's folder (Edit the task → Actions → Start in), or the log may land in Stata's program folder, which you usually can't write to (Stata then stops with r(608)).
Why the exit code lies
When a do-file stops at an error in batch mode, the error is in the log, but Stata itself does not report it to Windows as a failure: Task Scheduler still shows The operation completed successfully (0x0).
That's why the code above wraps your work in capture noisily { … }: the do-file catches its own error, sends /fail?rc= with Stata's return code (for example rc=601, file not found), and then stops with exit.
Put everything that can fail inside the braces. A command after the closing brace that fails is not caught.
Report the row count
The last line sends _N, the number of observations in memory when the do-file ends. If the dataset you care about is not the one in memory at the end, count it where it is:
use "C:\data\sales_clean.dta", clear
local tb_rows = _N
TaskBeacon alerts when the rows drop more than 40% below the recent runs or are 0. You can change both rules on the job page.
Common problems
- No ping at all.
shellrunscurl.exeon Windows (built into Windows 10 and 11) orcurlon macOS and Linux. Run the curl line in a Command Prompt or terminal on that machine; a proxy or firewall that blocks HTTPS shows up there. - A black window flashes (Windows). That is
shellopening a console for curl; it is harmless. - Start arrives but never the finish. The do-file stopped outside
capture noisily, or Stata was killed. After the grace period the job shows as Late. - Network drives. A task that runs while you are logged out doesn't see mapped drive letters like
S:; use UNC paths (\\server\share\…).
Use cases: Get an alert when a Stata do-file fails in batch mode. More questions: FAQ.