DEV Community

Philippe borribo
Philippe borribo

Posted on

Never get caught off guard: Receive your server's health every minute by Email

Introduction

For system administrators, surprises are rarely a good thing. Whether it's a sudden spike in CPU usage, memory running dangerously low, or disk space nearing its limit, these issues can quickly escalate into major problems : from system slowdowns to full outages.
The key to avoiding such disasters? Proactive monitoring.
Imagine receiving a quick health check of your server every single minute: delivered straight to your inbox. No need to log in and manually check metrics. Instead, you stay one step ahead, spotting trouble before it affects users or services.
In this article, you'll learn how to set up a simple but powerful script that runs automatically every minute. It collects key system metrics like CPU, memory, and disk usage, then emails the report to your system administrator. It's fast, lightweight, and incredibly effective - a perfect fit for anyone managing production environments or personal servers alike.

1. Why monitor your server every minute?

When it comes to server health, timing is everything. Even a few minutes of downtime can have serious consequences… from lost revenue to broken user trust. While traditional monitoring solutions may check system status every 5 or 10 minutes, some environments require faster feedback and immediate awareness.
a. Real-Time awareness
Monitoring your server every minute gives you near real-time visibility into its behavior. This is especially useful for detecting:
Sudden CPU spikes due to rogue processes.
Memory leaks that gradually degrade performance.
Rapid disk consumption caused by logging errors, backups, or attacks.
By checking these metrics every 60 seconds, you drastically reduce the time between a problem appearing and it being detected.
b. Faster incident response
Early detection means faster reaction. If you receive an email showing high CPU usage or critical memory shortage, you can investigate and act before the issue escalates into service disruption. In production environments, this can save hours of downtime and avoid emergency interventions.
c. Lightweight alternative to complex monitoring tools
While tools like Nagios, Zabbix, or Prometheus are powerful, they can be overkill for smaller setups or individual servers. A simple script combined with a cron job offers a minimalist approach to monitoring - no dashboards, no agents, no third-party services - just email notifications that work.
d. Ideal for headless or remote servers
If you're managing cloud instances, VPS setups, or physical servers in remote locations, having frequent performance reports in your inbox provides peace of mind. You're always informed, even when you're away from the terminal.

2. What metrics should be tracked?

To keep your server healthy and responsive, it's important to track a few key performance indicators. These metrics provide a snapshot of your system's current state and help you spot problems before they become critical.
Here are the most essential metrics you should include in your monitoring script:

a. CPU usage
CPU usage shows how much of your processor's capacity is being used at a given moment. High or constantly maxed-out CPU usage can indicate:
Inefficient code or processes running in loops
Malware or unauthorized scripts
Overloaded services during peak traffic

You can retrieve this metric using commands like:

top -bn1 | grep "Cpu(s)" 
Enter fullscreen mode Exit fullscreen mode

or more simply:

mpstat 1 1 
Enter fullscreen mode Exit fullscreen mode

b. Memory usage
Memory consumption is another critical metric. If your system runs out of RAM, it will start using swap space, which is significantly slower and can cause serious performance degradation.
Track:

  • Total memory

  • Used memory

  • Free memory

  • Swap usage

Command example:

free -m 
Enter fullscreen mode Exit fullscreen mode

c. Disk usage
Running out of disk space can crash applications, break databases, or prevent logging. Your script should report:
Total and used space per mounted partition
Alert thresholds (e.g., warn if a partition is over 90%)

Use this command:

df -h 
Enter fullscreen mode Exit fullscreen mode

d. Optional: load average
The load average reflects how many processes are actively running or waiting for CPU time. It gives a broader view of overall system strain.
Example:

uptime 
Enter fullscreen mode Exit fullscreen mode

or:

cat /proc/loadavg 
Enter fullscreen mode Exit fullscreen mode

e. Optional: Uptime and reboot detection
Knowing how long your server has been running helps identify unexpected reboots or instability.
Example:

uptime -p 
Enter fullscreen mode Exit fullscreen mode

3. Tools You'll Need

Setting up an automated monitoring script that sends email reports every minute doesn't require complex infrastructure or expensive software. All you need are a few standard tools, most of which are already available on typical Linux distributions. Here's a breakdown of what you'll need and why:

a. A Linux server
This guide assumes you're running a Unix-based system like Ubuntu, Debian, CentOS, or Red Hat. Most cloud instances (AWS, Azure, DigitalOcean, etc.) run some flavor of Linux. The commands and tools used are native to these environments.

b. A Scripting Language (Bash or Python)
You'll need a script that collects system metrics and formats them into an email message.
Bash is ideal for simplicity and direct access to system commands.
Python offers more flexibility, better formatting options, and error handling.
Choose the one you're more comfortable with - both are excellent for the task.

c. Crontab
cron is the time-based job scheduler built into Linux. It allows you to run scripts at fixed intervals… like every minute. 
To edit the scheduled jobs, use:

crontab -e 
Enter fullscreen mode Exit fullscreen mode

This is where you'll tell the system to execute your monitoring script regularly.

d. Mail Utility (mailx, msmtp, or sendmail)
To send system emails, your server needs a mail transfer agent (MTA) or a mail client that can send messages from the command line.
Some options include:
mailx: Simple and commonly pre-installed. Often used with sendmail or postfix.
msmtp: Lightweight and easy to configure with an external SMTP service (like Gmail).
sendmail or postfix: Full-fledged MTAs but heavier to configure.

For example, to install msmtp on Ubuntu:

sudo apt update sudo apt install msmtp msmtp-mta 
Enter fullscreen mode Exit fullscreen mode

Create a configuration file ~/.msmtprc

nano ~/.msmtprc 
Enter fullscreen mode Exit fullscreen mode

And add your SMTP credentials

defaults auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt logfile ~/.msmtp.log account gmail host smtp.gmail.com port 587 from your.email@gmail.com user your.email@gmail.com password your_app_password account default : gmail 
Enter fullscreen mode Exit fullscreen mode

e. A Valid Email Address for Delivery
In your script, you'll specify the recipient email.
Make sure:
The email is actively monitored.
SMTP settings are correct to ensure deliverability.
You whitelist your server's address, if needed, to avoid spam filters

4. Writing the Monitoring Script

Now that you know what metrics to track and which tools to use, it's time to write the core of the solution - the monitoring script. This script will collect the server's CPU, memory, and disk usage data, format it into a readable message, and send it to the system administrator via email.
We'll write a simple Bash script, which is efficient and widely compatible across Linux distributions.

a. Script Overview
The script will:

  1. Collect system performance metrics (CPU, RAM, Disk).
  2. Format the data into a plain-text report.
  3. Email the report to the system administrator.
  4. Optionally include a timestamp and hostname.

b. Sample script (monitor.py)

import psutil import socket import datetime import subprocess # Collect system metrics def get_metrics(): hostname = socket.gethostname() cpu = psutil.cpu_percent(interval=1) ram = psutil.virtual_memory() disk = psutil.disk_usage('/') now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") message = f"""\ Subject: [ALERT] Server Metrics - {hostname} From: Monitoring <monitor@localhost> To: your@email.com Date : {now} Server : {hostname} CPU : {cpu}% used RAM : {ram.percent}% used ({round(ram.used / (1024**3), 2)} GB / {round(ram.total / (1024**3), 2)} GB) Disk : {disk.percent}% used ({round(disk.used / (1024**3), 2)} GB / {round(disk.total / (1024**3), 2)} GB) """ return message # Send the email using msmtp def send_email(body): process = subprocess.Popen( ['msmtp', 'destination@email.com'], stdin=subprocess.PIPE, stderr=subprocess.PIPE ) stdout, stderr = process.communicate(input=body.encode()) if process.returncode != 0: print("Failed to send email:", stderr.decode()) else: print("Email sent successfully.") # Main if __name__ == "__main__": metrics = get_metrics() send_email(metrics) 
Enter fullscreen mode Exit fullscreen mode

c. Make the Script Executable
Save the script as monitor.py, then make it executable:
chmod +x monitor.py

d. Test the Script
Run it manually first to ensure it works as expected:

python monitor.py 
Enter fullscreen mode Exit fullscreen mode

Image description

Check the recipient inbox (or spam folder if necessary) to confirm receipt.

Image description

5. Automating the Script with Crontab

To send metrics every minute:

crontab -e 
Enter fullscreen mode Exit fullscreen mode

Then add:

* * * * * python monitor.py 
Enter fullscreen mode Exit fullscreen mode

Conclusion

Automating the monitoring of server health is not just a good practice, it's essential for maintaining system reliability, identifying performance issues early, and preventing downtime. With a simple Bash script, a few core Linux tools, and a properly configured email system, you can build a lightweight but powerful alerting mechanism that keeps your system administrator informed every minute.
This approach doesn't require expensive monitoring platforms or complex dashboards. It leverages native system utilities and the flexibility of cron to deliver real-time performance snapshots directly to the sysadmin's inbox. By tracking key metrics such as CPU usage, memory consumption, and disk space, you create a proactive culture of system care rather than waiting for something to break.
In environments where uptime and responsiveness matter, such a script can be the first line of defense… a quiet guardian running in the background, ensuring your infrastructure stays healthy and your admin stays informed.

Top comments (2)

Collapse
 
thegrumpyopsdude profile image
JB

I like the project, but watch out for alert/messaging fatiuge. So I would advise to only send an email when you actually need to troubleshoot/fix your server. Because having your email notification going off every minute will drive you insane.

Collapse
 
philippe_borribo profile image
Philippe borribo

Thanks for the comment, it's true that receiving an e-mail every minute is really crazy. The point of the article was just to show that you can see what's happening on your servers at any time (at your own pace).