Make Easy Email Server Statistics With A Simple Script

Last updated December 6th, 2023 00:10

If you manage a Linux server, in my case specifically Ubuntu, you might find it useful to have at least a basic overview of the server’s status. With a simple bash script, a few tools, and SSMTP, you can create automatically sent statistics about the server’s status. So, if you are interested in the server’s status, go ahead and create automated email statistics about the server’s status as well. After all, it’s really not difficult at all. So try to make easy email server statistics with a simple script with me, step by step.

Make Easy Email Server Statistics With A Simple Script

For the following example to work, you should have SSMTP and the systat component set up on your server. The principle is simple. First, you create a bash script that gathers statistics about the server’s operations using specific commands. Then, you use SSMTP to send these statistics to your email. To make this action regular, we will use CRON, where we will write a rule to automatically run the script once a day, for example, at 8:00 PM.

So, first, install and set up SSMTP. You can find a complete guide here: How to install and configure ssmtp on a Linux server

Create a bash script

Where you choose to save the script is up to you. For this example, let’s use a folder called “scripts“.

					mkdir /scripts

Now, in the “scripts” folder, create a bash script that we will name “” for the purpose of this example.

					sudo nano /scripts/

Insert the following code into it:


# Recipient's email address

# Get current date and time 24 hours ago
current_date=$(date +"%Y-%m-%d %H:%M:%S")
past_date=$(date -d "24 hours ago" +"%Y-%m-%d %H:%M:%S")

# Get disk usage information
disk_usage=$(df -h / | awk 'NR==2{print $5}')

# Get average RAM usage for the last 24 hours
average_ram_usage=$(free -t -s 86400 | awk '/^Average/{print $3/$2 * 100}')

# Get average CPU usage for the last 24 hours
average_cpu_usage=$(top -bn1 -d 86400 | awk 'NR>7{s+=$9}END{print s/NR}')

# Get top processes by CPU usage for the last 24 hours
top_processes=$(ps aux --sort=-%cpu | awk 'NR<=6{print $2, $3, $4, $11}' | sed 's/ /\t/g')

# Get the last 15 lines of the Apache error log
error_log=$(tail -n 15 /var/log/apache2/error.log)

# Compile email content
email_subject="Server Status Report for in the last 24 hours"
email_body="Disk usage: $disk_usage
Average RAM usage: $average_ram_usage%
Average CPU usage: $average_cpu_usage%
Top processes:

Last 15 lines of Apache error log:

# Send email using ssmtp
echo -e "Subject:$email_subject\nFrom:\nTo: $recipient\n\n$email_body" | ssmtp -t


Make Easy Email Server Statistics With A Simple Script

What does the script send?

Save the script. Now, whenever you run this script, it will generate the following statistics and send them to your email:

  • Disk usage: Information about disk usage on the server’s root directory (/).
  • Average RAM usage: Using the free command, it retrieves the average memory usage over the last 24 hours.
  • Average CPU usage: Using the top command, it retrieves the average CPU usage over the last 24 hours.
  • Top processes: A list of the most utilized processes on the server in the last 24 hours, obtained through the ps command.
  • Last 15 lines of the Apache error log: The script retrieves the last 15 lines of the Apache server’s error log using the tail command.

The entire content will be compiled into an email message, which will be sent to the provided address. The email will have the subject “Server Status Report for in the last 24 hours“, and the sender will be the address ““.

Set up the script to run periodically at 20:00 (8:00 PM).

Now, you need to arrange for the script to run periodically, specifically every day at 20:00 (8:00 PM), to have daily statistics about your server. Follow these steps:

  • Run the command crontab -e to edit the cron table for the current user.
  • If it’s your first time opening the cron table, you’ll be prompted to select a default text editor. Choose your preferred editor (e.g., nano, vim, emacs) and proceed.
  • In the opened editor, add the following line at the end of the file (adjust the path to the script according to your setup):
					0 20 * * * /scripts/

Make sure that the script “” is executable (using the command “chmod +x”) and that the path to it is correct. Cron will execute the script at the specified time and send the output to the respective user’s email address.

Make Easy Email Server Statistics With A Simple Script


And we’re done. You have created and saved the script, and CRON will regularly execute it every day at exactly 8:00 PM. What you add to the script depends on your needs. You can make it more concise or expand it to receive a comprehensive daily summary of information, allowing you to monitor not only disk usage but also various other activities.

For example, you could create a script to check disk usage with a condition. If the disk usage exceeds, for example, 85%, the script sends a warning email. You can use Cron to run this script every hour. Nothing will happen if the disk usage is below 85%, but if it exceeds 85%, a warning notification will be sent, indicating that you’re running out of disk space.

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 0 / 5. Počet hlasování: 0

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.


Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Upozornit na
0 Komentáře/ů
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje