← All guides

Guide · Cloud

Monitor an Azure Automation runbook

Azure Monitor can tell you a runbook job ended in Failed, but nothing pages you if the schedule gets disabled, a Hybrid Runbook Worker goes offline, or the run simply never starts. One HTTP call from inside the runbook closes that gap.

1. Create a check

Create a check in Mortemain matching the runbook's schedule and an appropriate grace window, then copy its unique ping URL.

 your ping URL
https://ping.mortemain.com/your-check-uuid

2. Check in from the runbook

Wrap the runbook's body in try/catch. On success, call Invoke-RestMethod against the ping URL. In the catch block, ping the /fail URL first, then rethrow so the runbook job itself still shows up as Failed in Azure.

 CleanupOldBlobs.ps1
$PingUrl = "https://ping.mortemain.com/your-check-uuid"

try {
    # authenticate the runbook with its managed identity
    Connect-AzAccount -Identity | Out-Null

    # ... your automation task ...
    Get-AzStorageBlob -Container "logs" -Context $ctx |
        Where-Object { $_.LastModified -lt (Get-Date).AddDays(-30) } |
        Remove-AzStorageBlob

    Invoke-RestMethod $PingUrl -TimeoutSec 10          # success
}
catch {
    Invoke-RestMethod "$PingUrl/fail" -TimeoutSec 10   # failure
    throw
}

Tip: store the ping URL as an Automation Variable asset (Get-AutomationVariable -Name 'MortemainPingUrl') instead of hardcoding it, so every runbook in the account can reuse or override it without a code change.

3. Schedule the runbook

Attach an Azure Automation schedule to the runbook as you normally would (Runbook → Schedules → Add a schedule), matching the cadence you set on the Mortemain check. The check-in is baked into the script itself, so it fires the same way whether the run was triggered by its schedule, a webhook, or a manual start.

Running on a Hybrid Runbook Worker instead of the Azure sandbox? Confirm the worker's network allows outbound HTTPS to ping.mortemain.com, since it's the worker's own egress rules that apply, not the cloud sandbox's.

4. Optional: track run duration

Ping /start right before the real work begins, and Mortemain reports actual run duration alongside up/down status, useful for spotting a runbook that still finishes, just slower every week:

 CleanupOldBlobs.ps1
Invoke-RestMethod "$PingUrl/start" -TimeoutSec 10   # started

# ... your automation task ...

Test it

Open the runbook and use Start (or the Test pane) to run it by hand. The check should flip to up within a few seconds of the run finishing. Then temporarily break something, a wrong resource group name works well, and rerun: the down alert should land immediately instead of waiting for the schedule's grace window to expire.