Get Help

CAT in cron

On Linux, CAT runs as the PowerShell module under PowerShell 7, and cron runs it on a schedule. Two traps, both about environment: cron starts with almost no environment variables, and the license key lives in the home of the user whose crontab it is.

Before you start

cron is a scheduler and CAT recognizes it: an unattended run needs the Enterprise plan and a license key — from CAT 3.0 a cron job without the key stops before any test runs — see Integrations. On Linux the tool is the PowerShell module (CAT CLI and the Python module are Windows-only); PowerShell 7 installed per Run CAT on Linux. The providers built on Windows components — Dax@*, PowerBI@*, CsvOleDB@1, ExcelOleDB@1 — are not available here; a cron job tests databases and files.

Prepare the user

As the user whose crontab will run the job:

pwsh -Command "Install-Module CAT -RequiredVersion 3.0.0 -AcceptLicense -Force -Scope CurrentUser   # the version you tested with - pin it"
pwsh -Command "Set-CatInstance -LicenseKey '<key>'"      # stored in ~/.config/CAT/.catconfig of THIS user

The script

cron runs commands with a minimal environment — no profile, no ~/.bashrc, usually not even your PATH. Put everything the run needs into one script and source the variables there:

# /opt/cat/run-dwh-tests.ps1
$ErrorActionPreference = 'Stop'
# the project file reads these as %DWH_CONNECTION_STRING% etc. - keep the real file readable by this user only
Get-Content /opt/cat/dwh.env | ForEach-Object { $n, $v = $_ -split '=', 2; [Environment]::SetEnvironmentVariable($n, $v) }
Import-Module CAT
Invoke-CatProject -Path /opt/cat/DWH/DwhTests.cat.yaml -LoggingLevel Error
$s = Get-CatTestResultSummary
if ($s.FailedCount + $s.ErrorCount -gt 0) { exit 1 }   # make the job's exit code say it

Run it once by hand as that user — pwsh /opt/cat/run-dwh-tests.ps1 — before you schedule it. Variable names are case-sensitive on Linux; the project file must use exactly the casing of the .env file.

The crontab line

crontab -e as that user:

# every day at 06:15
15 6 * * * /usr/bin/pwsh -NoProfile -File /opt/cat/run-dwh-tests.ps1 >> /var/log/cat/dwh-tests.log 2>&1

Full paths everywhere — which pwsh tells you where it is — and a log redirect, because cron otherwise mails the output or drops it.

Results

Give the project outputs — JSON, YAML, JUnit or a database table; the Excel writer needs libgdiplus on Linux. The script above turns failed tests into exit code 1, which is what a monitoring tool watching the cron job sees; CAT’s own exit code, as everywhere, is 0 when the run completed — see Results and exit codes.