Module 1 · Project 01 · Linux Server Automation Toolkit · Lesson 4 of 48
Schedule with cron and send alerts
Run your scripts automatically on a schedule, log what they find, and get notified when something crosses a limit.
In this lesson · about 30 minutes
- Read and write the five fields of a cron schedule
- Install a cron job with crontab -e and capture its output
- Build a threshold alert: check, log, notify, without spamming
cron: the server's alarm clock
cron is a background service on almost every Linux server. It wakes up every minute, reads a list of jobs, and runs the ones that are due. Each user has a list called a crontab; edit yours with crontab -e and view it with crontab -l.
Each line is five time fields, then the command:
# ┌ minute (0–59)
# │ ┌ hour (0–23)
# │ │ ┌ day of month (1–31)
# │ │ │ ┌ month (1–12)
# │ │ │ │ ┌ day of week (0–7, 0 and 7 are Sunday)
# │ │ │ │ │
*/5 * * * * /opt/toolkit/scripts/check-cpu.sh >> /var/log/toolkit/cpu.log 2>&1
0 2 * * * /opt/toolkit/scripts/backup.sh /var/www
*means every value;*/5means every 5th;1-5is a range;1,15is a list.- cron runs with a very small environment and no terminal. Use full paths everywhere, and send output somewhere:
>> file.log 2>&1appends both normal output and errors to a log.
Try it · watch it run
Cron schedule explorer
Pick a preset or type your own schedule. The grid shows every hour of one week; press Play the week to watch the job fire.
*/5
minute
0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55
*
hour
every
*
day of month
every
*
month
every
*
day of week
every
Every 5 minutes: a health check
One week, hour by hour
2016 runs this week
Next runs after Mon 16 Nov, 08:00
- Mon 16 Nov 08:00
- Mon 16 Nov 08:05
- Mon 16 Nov 08:10
- Mon 16 Nov 08:15
- Mon 16 Nov 08:20
- Mon 16 Nov 08:25
Whiteboard · watch and try
Decode a cron line
Press Play to read 30 2 * * 1-5 field by field.
Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.
From a number to a notification
Now put the pieces together. cron runs a check every few minutes. The script compares the CPU with a limit, writes a line to a log either way, and only when the limit is crossed sends a message.
#!/bin/bash
set -euo pipefail
source /opt/toolkit/config/toolkit.conf # CPU_LIMIT, WEBHOOK_URL
CPU=$(top -bn1 | awk '/^%Cpu/ {print int(100 - $8)}') # 100 − idle
NOW=$(date '+%F %T')
if [ "$CPU" -gt "$CPU_LIMIT" ]; then
echo "$NOW WARN cpu=$CPU% limit=$CPU_LIMIT%"
curl -fsS -X POST -H 'Content-Type: application/json' \
-d "{\"text\": \"$(hostname): CPU at $CPU%\"}" "$WEBHOOK_URL"
exit 1
fi
echo "$NOW INFO cpu=$CPU%"
The notification here is a webhook: a URL that chat apps such as Slack, Teams or Discord give you so programs can post messages. Email (mail) works too. Keep the URL in the config file, never in the script you push to Git, because anyone with the URL can post to your channel.
One snag: if CPU stays high for an hour and you check every 5 minutes, you get 12 identical alerts. People start ignoring noisy alerts (alert fatigue). A simple fix is a state file: write /tmp/cpu-alerting when you alert, skip further alerts while it exists, and delete it (sending "recovered") once CPU drops.
Try it · watch it run
From CPU spike to alert
Cause a spike and watch each stage light up: cron tick, script, compare, log, alert. Turn off 'Alert once' to see alert fatigue happen.
web-01 CPU, last hour
08:00 · 24%
Dashed line: your alert limit. A check runs every 5 simulated minutes.
(nothing logged yet: CPU is under the limit)
# ops-alerts
No alerts. Cause a spike to see one arrive.
0 checks so far
Hands-on lab (simulated terminal)
Lab: install and verify a cron job
Schedule the CPU check every 5 minutes for the deploy user.
Step 1 of 5. Type each command and press Enter. Nothing runs on your computer.
1. List your current cron jobs.
Check your understanding0 of 3 correct
Key terms
Say the meaning to yourself, then tap a card to check.
Quiz score: 0 of 3
Written by Noviq Academy in its own words. The labs are simulations: no real servers are touched. When you build the projects yourself, use a practice virtual machine or a free-tier cloud account you control, watch your cloud spending, and never run cleanup or delete commands on a machine you care about. Tool names belong to their owners; versions and output are shown for learning and may differ slightly from yours. Read-aloud and the whiteboard voice use your device's built-in voices.