Reliability & Infrastructure

    Monitoring PowerShell & Bash Scripts with Heartbeats

    Add a single curl line to any script and know instantly when it fails, hangs, or finishes late.

    6 min readGuide

    The Problem with Unmonitored Scripts

    PowerShell scripts on Windows servers and Bash scripts on Linux boxes power everything from log rotation to certificate renewal. These scripts typically run via Task Scheduler or cron with no visibility beyond local log files. When they fail — wrong exit code, hung process, or a server that simply didn't boot — nobody notices until the downstream effect surfaces as a customer complaint.

    Adding a Heartbeat Ping to Any Script

    The integration is a single line at the end of your script. The key principle: only ping on success. If your script exits early due to an error, the ping never fires and FourSight alerts you.

    Bash / Shell

    Add a curl call after the main logic. Use the && operator so the ping only fires if the preceding command succeeds.

    PowerShell

    Use Invoke-RestMethod at the end of your script, wrapped in your existing error handling.

    # PowerShell — ping only on success
    try {
        # … your script logic …
        Invoke-RestMethod -Uri "https://ping.foursight.cloud/hb/<YOUR_TOKEN>" -Method Get
    } catch {
        Write-Error "Script failed: $_"
        exit 1
    }

    Bash Example with Error Handling

    For Bash scripts, trap errors and only ping after the critical section completes.

    #!/usr/bin/env bash
    set -euo pipefail
    
    # … your script logic …
    
    # Ping on success
    curl -fsS --retry 3 --max-time 10 https://ping.foursight.cloud/hb/<YOUR_TOKEN>

    Monitoring a Commercial SaaS?

    FourSight includes 25 commercial-safe monitors with multi-region validation.

    Start Monitoring Free

    Choosing the Right Schedule Mode

    If your Task Scheduler job runs 'Daily at 06:00', use cron-expression mode with '0 6 * * *'. If your script is triggered by an event but should run at least once every N minutes, use fixed-interval mode. Set a grace period that accounts for your script's maximum expected runtime plus a safety margin.

    Handling Long-Running Scripts

    For scripts that take minutes or hours, send the ping only after completion. Set your grace period to the expected runtime plus 50%. For extremely long jobs (e.g. a 4-hour data migration), consider sending a start ping to a separate heartbeat monitor so you can detect both 'never started' and 'started but didn't finish' failure modes.

    Alerting & Escalation

    Pair your heartbeat monitor with a notification rule so failures reach the right person. A missed nightly backup ping at 3:15 AM should page the on-call engineer, not send a Slack message to a channel nobody watches at night.

    Protect Your SaaS Revenue

    Start monitoring in under 60 seconds.