All-in-One Shell Script for basic health check of Debian/Ubuntu OS

Dr. Saket Acharya
2 min readJun 18, 2024

--

Hi, All; I am sharing an AIO shell script written in Bash that provides detailed insights into the performance and health of a Linux system. It gathers information such as:

Process Analysis: Identifies and displays the process with the highest CPU usage.System Information: Fetches detailed system information, including the machine’s hostname, operating system, kernel version, and architecture.
Network Configuration: Displays the IP address of the machine.
User Activity: Provides information on the last logged-in user.
Process Details: Displays detailed metrics of the highest CPU usage process, including virtual memory size, resident set size, and memory percentage.
Disk Usage: Shows the disk usage details using the df -h command.
Memory Usage: Reports total system memory usage and memory usage for the highest CPU usage process using the free -h command.

Automating these diagnostics enhances system management and troubleshooting efficiency, saving time and minimizing manual intervention by typing multiple commands.

The script can be downloaded from my GitHub repo using the link below.

A detailed description of the script is given below:

Identify the process with the highest CPU usage:

ps H -eo pid,pcpu — sort=-pcpu | head -n 2 | tail -n 1 | awk ‘{print $1}’:

Lists all processes with their PID and CPU usage sorted by CPU usage in descending order.

Selects the highest CPU-consuming process using line manipulation commands like head -n 2 and tail -n 1.

Extracts the PID using awk.

Check if the process was found:

if [ -z “$high_cpu_pid” ]: Checks if the PID variable is empty, indicating no process was found.

Display system information:

hostnamectl: Command that displays detailed system information.

Display the IP address:

hostname -I: Retrieves the IP address.

Display the last logged-in user:

last -1: Retrieves the most recent login information.

Display the header for the ps aux output:

ps aux | head -n 1: Outputs the column headers for the ps aux command.

Display details of the highest CPU-consuming process:

ps aux | grep -w “$high_cpu_pid” | grep -v grep: Filters the output for the specific PID and excludes the grep process itself.

Display system disk usage:

df -h: Retrieves disk usage in a human-readable format.

Display total system memory usage:

free -h: Retrieves the total, used, and free memory in a human-readable format.

Display memory usage for the highest CPU-consuming process:

ps -p $high_cpu_pid -o pid,vsz,rss,%mem,cmd: Displays detailed memory metrics for the highest CPU-consuming process.

--

--

Dr. Saket Acharya

I am a cybersecurity enthusiast with a keen interest in VAPT, Threat Intelligence, Kubernetes Security, DevSecOps. I like scripting and automating the tasks.