• Contact
  • About Me
  • Privacy Policy
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Linux Tutorials

Automating Linux Server Health Checks with a Bash Script

neo by neo
May 27, 2025
in Linux Tutorials
0
Automating Linux Server Health Checks with a Bash Script

Automating Linux Server Health Checks with a Bash Script

0
SHARES
78
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Automating Linux Server Health Checks with a Bash Script

Automating Linux server health and performance checks with a Bash Script for example is a crucial task for system administrators and operations engineers. Regular checks help ensure that systems are running optimally, hardware issues are identified early, and potential problems are addressed before they escalate. However, manually performing these checks can be time-consuming and error-prone, especially when managing multiple servers.

Automation provides an efficient solution. In this article, we’ll show you how to use a simple yet powerful Bash script to automate essential server health checks. This script gathers critical information such as system details, CPU and memory usage, disk space, network activity, and more, providing a comprehensive snapshot of your server’s status in one execution.

Whether you’re troubleshooting issues, preparing for audits, or simply want peace of mind knowing your Linux server is in good shape, this script can save you time and effort. Read on to learn how it works and how you can customize it for your specific needs.

ADVERTISEMENT

Purpose of the Script

Having quick and reliable access to system information is crucial for maintaining server health and performance. This Bash script is designed to automate the collection of essential system metrics, such as CPU usage, memory usage, disk space, and network information.

Which informations are Collected

  • CPU Information:
    • Model, cores, and speed.
lscpu
cat /proc/cpuinfo
  • RAM Memory Information:
    • Total and available RAM.
free -h
  • CPU Usage (1, 5, 15 Minutes):
    • Load averages.
uptime
  • RAM Memory Usage:
    • Detailed RAM usage.
free -h
  • HDD Usage (Capacity, Usage, Free):
    • Disk space usage.
df -ih
  • Network Information:
    • IP addresses and interface status.
ifconfig
# or
ip a

Additional Information to Consider

  • Process Information:
    • List top memory and CPU consuming processes.
top -b -n 1 | head -n 20
  • Swap Usage:
    • Swap space details.
swapon --show free -h
  • Temperature Monitoring -> it will not work on all servers!
    • You need to install lm-sensors package
      • sudo apt install lm-sensors !
    • CPU and system temperatures (if sensors are available).
# Install package
sudo apt install lm-sensors

# Check the output
sensors
  • Service Status:
    • Status of critical services (e.g., web server, database).
systemctl status <service_name>
  • Network Connections:
    • Current network connections and listening ports.
netstat -tuln # or ss -tuln
  • Uptime and System Load:
    • Detailed uptime and load averages.
uptime
  • Filesystem Inode Usage:
    • Inode usage for each mounted filesystem.
df -i
  • Dmesg Logs:
    • Recent kernel logs for hardware and driver messages.
dmesg | tail -n 20
  • User Logins:
    • Current user sessions and recent login attempts.
who # or
last
  • Installed Updates:
    • List of installed updates and available updates.
apt list --upgradable

Script for Health Check

Save the script:

nano sysreport.sh
#!/bin/bash

echo "==== System Information ===="
echo "Hostname: $(hostname)"
echo "Date and Time: $(date)"
echo

echo "==== CPU Information ===="
lscpu | grep -E '^Model name|^CPU\(s\):|^Thread|^Core|^Socket|^NUMA|^CPU MHz|^Architecture'
echo

echo "==== RAM Information ===="
free -h
echo

echo "==== CPU Load Averages ===="
uptime
echo

echo "==== Disk Usage ===="
df -h
echo

echo "==== Swap Usage ===="
swapon --show
echo

echo "==== Network Interfaces ===="
ip a
echo

echo "==== Top Processes ===="
top -b -n 1 | head -n 20
echo

echo "==== Current Network Connections ===="
ss -tuln
echo

echo "==== Temperature ===="
sensors
echo

echo "==== Service Status (e.g., ssh) ===="
systemctl status ssh --no-pager
echo

echo "==== Recent Kernel Messages ===="
dmesg | tail -n 20
echo

echo "==== Recent Logins ===="
last -n 5
echo

echo "==== Available Updates ===="
apt list --upgradable
echo

Make it executable:

chmod +x sysreport.sh

Execute the script:

bash sysreport.sh

Daily run the system report script

If you need your daily Systemreport check use the following script and the Crontab:

ADVERTISEMENT
nano sysreport.sh
#!/bin/bash

LOG_DIR="/opt/scripts/log"
LOG_FILE="$LOG_DIR/sysreport_$(date +%F).log"

# Ensure the log directory exists
mkdir -p "$LOG_DIR"

exec > "$LOG_FILE" 2>&1

echo "==== System Information ===="
echo "Hostname: $(hostname)"
echo "Date and Time: $(date)"
echo

echo "==== CPU Information ===="
lscpu | grep -E '^Model name|^CPU\(s\):|^Thread|^Core|^Socket|^NUMA|^CPU MHz|^Architecture'
echo

echo "==== RAM Information ===="
free -h
echo

echo "==== CPU Load Averages ===="
uptime
echo

echo "==== Disk Usage ===="
df -h
echo

echo "==== Swap Usage ===="
swapon --show
echo

echo "==== Network Interfaces ===="
ip a
echo

echo "==== Top Processes ===="
top -b -n 1 | head -n 20
echo

echo "==== Current Network Connections ===="
ss -tuln
echo

echo "==== Temperature ===="
sensors
echo

echo "==== Service Status (e.g., ssh) ===="
systemctl status ssh --no-pager
echo

echo "==== Recent Kernel Messages ===="
dmesg | tail -n 20
echo

echo "==== Recent Logins ===="
last -n 5
echo

echo "==== Available Updates ===="
apt list --upgradable
echo

Make it executable:

chmod +x sysreport.sh

Update the cron job: Now, simply call the script from the cron job without handling the log file in the cron command:

0 6 * * * /opt/scripts/log/sysreport.sh

Automating Linux Server Health Checks with a Bash Script

ADVERTISEMENT

defencedev.com

About The Author

neo

See author's posts

Tags: bash-scripting
ADVERTISEMENT
Previous Post

How to Check Time and Change Timezone in Ubuntu

Next Post

Fortinet FortiGate: Port Forwarding for HTTPS NAT Configuration

neo

neo

Next Post
Fortinet FortiGate: Change Default Administration HTTPS and HTTP Port

Fortinet FortiGate: Port Forwarding for HTTPS NAT Configuration

Follow Us

  • Trending
  • Comments
  • Latest
MikroTik: Export Configuration in Text File

MikroTik: Export Configuration in Text File

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Immich: Installation on Ubuntu 22.04

Immich: Installation on Ubuntu 22.04

Fortinet FortiGate Upgrade Path Tool

Fortinet FortiGate Upgrade Path Tool

NextCloud: Port Forwarding - Essential Ports for Smooth Functionality

NextCloud: Port Forwarding – Essential Ports for Smooth Functionality and Remote Access

Organizing and Managing Photos with Immich: Features I Use

Organizing and Managing Photos with Immich: Features I Use

Install Ubuntu 22.04 on Oracle VM Virtual Box

Install Ubuntu 22.04 on Oracle VM Virtual Box

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Browse by Category

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.