Back to Blog
How to Monitor Linux CPU Usage with Cron

How to Monitor Linux CPU Usage with Cron

   Mariusz Antonik    Automation    6 min read    4 views

If you run a small Linux server, CPU problems rarely announce themselves with a neat warning. They usually show up as a checkout page that feels slow, a backup job that starts missing its window, or a database query that suddenly takes longer during business hours. A simple scheduled check can give you a useful early signal before those symptoms become urgent.

The goal is not to replace a full monitoring platform. The goal is to collect enough CPU evidence every few minutes to answer three practical questions: is the server busy, is the pressure sustained, and which process was active when the pressure started? For many small teams, that history is the difference between guessing and fixing the right thing.

What CPU data is worth collecting?

Start with a short list. Load average tells you whether runnable work is backing up. CPU idle percentage tells you whether the processor is actually saturated. A quick process snapshot tells you which workload was consuming time at the moment of the sample.

For a single small server, the most useful fields are usually:

  • Timestamp: always log in a consistent timezone, preferably UTC or the server's operating timezone.
  • 1, 5, and 15 minute load average: this helps separate a quick spike from a sustained trend.
  • CPU idle percentage: low idle time for several samples in a row is more concerning than one busy moment.
  • Top CPU processes: collect the process name, PID, and CPU percentage so the log is actionable.
  • Core count: load average only makes sense when compared with available CPU cores.

A lightweight bash script for scheduled checks

Create a small script that writes one structured line per run. The example below avoids heavy dependencies and works well as a starting point on common Linux distributions.

#!/usr/bin/env bash
set -euo pipefail

LOG_DIR="/var/log/server-health"
LOG_FILE="$LOG_DIR/cpu-samples.log"
mkdir -p "$LOG_DIR"

TS=$(date -Iseconds)
CORES=$(nproc)
LOAD=$(awk '{print $1","$2","$3}' /proc/loadavg)
CPU_IDLE=$(top -bn1 | awk -F'id,' '/Cpu\(s\)/ { split($1, a, ","); print a[length(a)] }' | xargs)
TOP_PROCESSES=$(ps -eo pid,comm,%cpu --sort=-%cpu | head -n 6 | tail -n 5 | tr '\n' ';')

printf '%s cores=%s load=%s cpu_idle="%s" top="%s"\n' \
  "$TS" "$CORES" "$LOAD" "$CPU_IDLE" "$TOP_PROCESSES" >> "$LOG_FILE"

Save it as /usr/local/sbin/cpu-sample.sh, then make it executable:

sudo chmod 750 /usr/local/sbin/cpu-sample.sh
sudo mkdir -p /var/log/server-health

Run it manually once before adding a schedule. If the command writes a line to /var/log/server-health/cpu-samples.log, you have a working baseline.

Schedule the check with cron

For most small production servers, a five-minute interval is a good balance. It is frequent enough to catch repeated pressure but not so frequent that the monitoring itself becomes noisy.

*/5 * * * * /usr/local/sbin/cpu-sample.sh >/dev/null 2>&1

If the server is extremely small, start with every ten or fifteen minutes. If you are investigating an active issue, temporarily sample every minute for a short window, then return to a normal interval after the incident is understood.

Use thresholds as prompts, not panic buttons

A common mistake is treating one high sample as an emergency. CPU monitoring is more useful when it highlights patterns. A one-minute load spike during a deployment may be normal; a 15-minute load average that stays above the number of CPU cores deserves attention.

As a simple rule of thumb, review the server when:

  • The 15-minute load average is consistently near or above the CPU core count.
  • CPU idle time stays very low across multiple samples.
  • The same process appears at the top of the list during several busy windows.
  • Spikes align with cron jobs, backups, report generation, imports, or traffic peaks.

These thresholds should start conversations. They help you ask better questions: did a new background job overlap with backups, did a database query change, or did traffic grow beyond the current instance size?

Keep logs small and readable

Any scheduled log needs rotation. Without it, a helpful file can slowly become a disk-space problem. Add a simple logrotate rule such as:

/var/log/server-health/cpu-samples.log {
    weekly
    rotate 8
    compress
    missingok
    notifempty
    create 0640 root root
}

This keeps several weeks of compressed history while preventing the log from growing forever. If you are collecting data for a weekly operations review, eight weeks gives you enough trend context to spot recurring pressure.

Turn raw samples into weekly decisions

The real value of CPU history is not the log line itself. The value is the decision it supports. Once a week, summarize the busiest windows, the most frequent top processes, and whether load is trending up or down.

For example, a useful weekly note might say: "CPU load was healthy most of the week, but the 15-minute load average exceeded four cores during nightly imports on Tuesday and Thursday. The top process was the reporting worker, so the next action is to move that job outside the backup window." That kind of finding is small, practical, and much easier to act on than a dashboard full of unexplained spikes.

When cron-based checks are enough

This approach is a good fit when you need basic visibility, have only a few servers, or want a backup signal that continues working even if a larger monitoring stack is unavailable. It is also useful for developers managing client infrastructure where simple evidence matters more than complex tooling.

It is not a complete incident response system. If you need real-time paging, multi-node dashboards, anomaly detection, or detailed application traces, use a dedicated monitoring platform. Even then, simple scheduled CPU samples can still provide a helpful independent trail when diagnosing long-running performance issues.

A practical next step

Pick one low-risk server and collect CPU samples for a week. At the end of the week, look for repeated busy periods and match them against deployments, backups, imports, traffic peaks, and database maintenance. If you find the same pattern twice, you have a useful optimization target.

Small, steady visibility beats occasional panic. A scheduled CPU check gives you just enough history to notice capacity pressure early, explain performance changes clearly, and decide whether to tune a job, resize a server, or investigate an application bottleneck.

Want weekly infrastructure health checks without dashboard fatigue?

DMCloud Architect sends Linux and MySQL infrastructure health reports directly to your inbox, so you can spot risks early without adding another monitoring dashboard to watch.

Get the free starter plan for weekly infrastructure health reports.

About the Author
Mariusz Antonik

Oracle Cloud Infrastructure expert and consultant specializing in database management and automation.

All Tags
#Advanced #alerts #automation #Bash #bash cpu monitoring script #bash monitoring #bash scripting #Beginner #Best Practices #block volume backup #Capacity Planning #cloud backup strategy #cloud-database-setup #cloud-networking #compute #cpu bottleneck #CPU Monitoring #cpu monitoring linux #cpu monitoring script linux #cpu trends #cpu usage trends #cpu usage trends linux #create oracle db system in oci #cron #cron cpu monitoring #cron cpu monitoring linux #cron jobs #database #database monitoring #database performance #database-setup #detect slow queries mysql #devops #disk capacity planning server #disk forecasting linux #disk growth trend linux #Disk Monitoring #disk usage #disk usage script linux #disk usage trends #Early Detection #easy infrastructure monitoring #firewall-rules #free-tier #Guide #health dashboards #Health Reporting #historical server monitoring #how to monitor cpu usage linux #infrastructure #infrastructure health #infrastructure health dashboard #infrastructure health reporting #infrastructure monitoring #infrastructure monitoring report #infrastructure trends #infrastructure trends monitoring #Infrastructure Visibility #infrastructure-checklist #ip-allowlist #lightweight linux monitoring #lightweight monitoring #linux #linux administration #linux cpu monitoring #linux cpu usage #linux disk capacity planning #linux disk usage #Linux monitoring #linux monitoring setup #linux monitoring tools #linux performance #linux performance monitoring #linux server #linux server monitoring #linux servers #linux storage #linux tools #low maintenance monitoring #monitor cpu usage over time linux #monitor linux server health #monitor server trends #monitor small production server #monitoring #monitoring without complexity #MySQL #mysql health reporting #MySQL monitoring #mysql optimization #MySQL Performance #mysql performance degradation #mysql performance monitoring #mysql performance trends #mysql query performance issues #mysql server monitoring #mysql slow queries #mysql slow query analysis #mysql slow query monitoring #mysql trends #mysql-health #mysql-heatwave #networking #nsg #OCI #oci backup #oci bastion tutorial #oci block volume #oci infrastructure as code #OCI monitoring #oci networking #oci oracle database private subnet setup #oci oracle database tutorial #oci security #oci setup guide #oci terraform tutorial #oci tutorial for beginners #oci vcn terraform #oci virtual machine db system guide #oci-database #oci-mysql-heatwave #oci-mysql-heatwave-tutorial #oci-subnets #oracle base database service tutorial #oracle cloud bastion #oracle cloud free tier tutorial #oracle cloud infrastructure step by step #oracle cloud infrastructure tutorial #oracle cloud storage #oracle database on oci setup #oracle-cloud #oracle-cloud-mysql-database-service #oracle-cloud-mysql-setup #oracle-cloud-vcn-setup #Performance #Performance Degradation #performance monitoring #performance trend monitoring #performance trends #plan disk growth server #practical server monitoring #predict disk usage growth #private instance access #query optimization #rollback #route-tables #Security #security lists #server #server health #server health reporting #server health weekly report #server monitoring #Server Performance #server trend analysis #server-security #server-trends #servers #simple cpu monitoring linux #simple linux monitoring #simple monitoring small business #simple monitoring system #simple ops monitoring #slow queries #slow query reporting mysql #small business infrastructure #small business IT #small business servers #small infrastructure monitoring #small server monitoring #ssh bastion #storage capacity planning linux #storage monitoring #subnets #System Health #system health reporting #terraform oci compute #terraform oracle cloud infrastructure #Trend Monitoring #trend-analysis #trends #Tutorial #uptime-monitoring #vcn #vcn-design