This guide shows you how to build a simple, low-cost alerting system that watches your application logs for signs of a failed deployment. You'll set up a script that scans recent logs for error patterns and sends a notification (email, Slack, or similar) when something looks wrong. This catches issues like crash loops, database connection failures, or unexpected exceptions right after you ship new code, without needing expensive monitoring tools.
What You'll Need
- A server or machine where your application logs are written (or accessible via SSH).
- Access to a command line (Linux/macOS recommended; Windows with WSL works too).
- A way to send notifications: a free Slack workspace, a Gmail account for SMTP, or a simple webhook service.
- Basic familiarity with shell scripting and cron (or Task Scheduler on Windows).
Step-by-Step
- Identify your log location and format. Find where your application writes logs. Common places:
/var/log/myapp/,~/logs/, or a centralized log file likeapplication.log. Note the naming convention (e.g.,app-2025-01-01.log). - Define what a deployment failure looks like. Look at past incidents or common error signatures. Typical patterns include:
ERROR,Exception,Connection refused,OutOfMemoryError,Failed to start, or a specific exit code in your startup script. Write these as grep patterns, one per line in a file calledpatterns.txt. - Create the alert script. Write a bash script (e.g.,
check_deploy.sh) that does the following:
This script finds log files changed since the last run and greps them for your patterns. It prints matches to stdout, which you can then pipe to a notification command.#!/bin/bash LOG_DIR="/var/log/myapp" PATTERN_FILE="/path/to/patterns.txt" LAST_RUN="/tmp/last_check_time" ALERT_EMAIL="you@example.com"Get the timestamp of the last run (default to 10 minutes ago)
if [ -f "$LAST_RUN" ]; then SINCE=$(cat "$LAST_RUN") else SINCE=$(date -d '10 minutes ago' +'%Y-%m-%d %H:%M:%S') fi echo "$(date +'%Y-%m-%d %H:%M:%S')" > "$LAST_RUN"Find log files modified since last check
find "$LOG_DIR" -name '*.log' -newermt "$SINCE" -print0 | while IFS= read -r -d '' file; doSearch for patterns, ignore case, output matches
grep -iEf "$PATTERN_FILE" "$file" | while read -r line; do echo "ALERT: $file: $line" done done - Add a notification method. Replace the
echoline with a real notification. For email viamailcommand:echo "$line" | mail -s "Deploy alert" $ALERT_EMAIL. For Slack, use a webhook:curl -X POST -H 'Content-type: application/json' --data '{"text":"Deploy alert: '$line'"}' YOUR_SLACK_WEBHOOK_URL. For a simple push notification, use a service like Pushover or ntfy.sh. - Make the script executable and test it. Run
chmod +x check_deploy.sh. Then manually run it with./check_deploy.shand verify it picks up recent errors. If you don't have recent errors, create a test log entry:echo "ERROR: test failure" >> /var/log/myapp/test.logand run again. - Schedule it with cron. To run every 5 minutes, add to your crontab (
crontab -e):*/5 * * * * /path/to/check_deploy.sh >> /var/log/alert.log 2>&1. This logs the script's output for debugging. Adjust the interval based on how quickly you need to know about failures. - Set up a deployment hook (optional but recommended). Instead of only checking periodically, you can trigger the script right after a deployment. For example, in your CI/CD pipeline (like Jenkins, GitLab CI, or a simple deploy script), add a step:
./check_deploy.sh --after-deploy. Modify the script to accept a flag that forces a check of all logs since the deployment start time.
Common Mistakes
- Not handling log rotation. If your logs are rotated (e.g., daily), the
find -newermtmay miss files that were archived. Make sure your script also checks the most recent file regardless of modification time. - False positives from old errors. If you don't track the last check time correctly, you'll re-alert on the same errors every run. Ensure the timestamp file is updated before the grep runs, not after.
- Using overly broad patterns. A pattern like
ERRORmight match harmless messages like "Error handling user input" that are not deployment failures. Refine patterns to be specific to critical failures. - Ignoring exit codes. Your script might fail silently if the log directory doesn't exist or permissions are wrong. Add
set -eor check for errors and log them.
Tips
- Start with a few patterns. Begin with 3-5 high-signal patterns and add more as you learn what matters.
- Include the log file name and timestamp in your alert. This makes it easier to investigate.
- Use a dedicated notification channel. Create a separate Slack channel or email folder for deployment alerts so they don't get lost.
- Add a cooldown. To avoid alert storms, suppress duplicate alerts for the same pattern within a certain time window. You can store the last alert time per pattern in a file.
- Test your alert script during a real deployment. Run it manually right after a deploy to see if it catches issues you expect.
Troubleshooting
- No alerts are being sent. Check the script's output by running it manually. Verify the notification command works independently (e.g., test
mailorcurl). Check the cron log (/var/log/syslogorcron.log) for errors. - Alerts are sent for every run, even when nothing is wrong. This usually means the timestamp file isn't being updated. Check permissions on the
/tmpdirectory or the path you're using. Ensure the script has write access. - Script runs but finds no logs. Verify the
LOG_DIRpath is correct and that log files exist. Usefind "$LOG_DIR" -name '*.log' -newermt "$SINCE"manually to see if it returns anything. - Alert message is empty. Your grep might be matching lines that are empty or only whitespace. Add
grep -v '^$'to filter empty lines, or check the pattern file for trailing spaces. - Email notifications go to spam. If using Gmail SMTP, you may need to allow less secure apps or use an app-specific password. Consider using a transactional email service like SendGrid free tier for better deliverability.
Frequently Asked Questions
Can I use this with Windows?
Yes, but you'll need to adapt the script to PowerShell or use WSL to run bash scripts. Task Scheduler can replace cron.
What if my logs are in JSON format?
You can still use grep, but you might want to use jq to parse specific fields. For example, jq -r 'select(.level=="ERROR") | .message'.
How often should I run the check?
It depends on your tolerance for downtime. Every 5 minutes is a good balance for most apps. If you need faster, consider triggering on log write events using something like inotifywait.
Can I get alerts for specific error codes?
Yes, add patterns like HTTP 500 or Exit code: 1 to your patterns file. Just be careful about false positives.
Is this reliable enough for production?
For basic monitoring, yes. But it's not a substitute for a full APM tool. It's a cost-effective first line of defense. Make sure your script is robust (handles missing files, permissions, etc.) and test it regularly.
How do I avoid alert fatigue?
Use specific patterns, add a cooldown mechanism, and only alert on patterns that indicate a real deployment failure, not general application errors that happen during normal operation.