Operations 6 min read

Master Practical Bash Scripts for System Monitoring and Automation

Discover a collection of practical Bash script snippets covering random string generation, colored output functions, batch user creation, package and service checks, host reachability, CPU/memory/disk monitoring, remote disk usage collection, and website availability testing, with essential scripting best‑practice tips.

ITPUB
ITPUB
ITPUB
Master Practical Bash Scripts for System Monitoring and Automation

This guide presents a series of useful Bash script examples that are frequently asked in technical interviews and valuable for everyday system administration tasks.

General Scripting Guidelines

Start scripts with the interpreter line #!/bin/bash.

Use four‑space indentation and add ample comments.

Adopt naming conventions: uppercase for constants, lowercase for local variables, lowercase for functions, and names that reflect purpose.

Variables are global by default; use local inside functions to avoid polluting other scopes.

Debug with set -e (exit on non‑zero status) and set -x (trace execution).

Always test scripts thoroughly before deploying to production.

1. Generate Random Strings or Numbers

Example commands (shown in the accompanying images) produce an 8‑character random alphanumeric string or an 8‑digit random number.

Random string example
Random string example
Random number example
Random number example

2. Colorful Output Function

A reusable function that prints text in various colors using ANSI escape codes. The definition can include the function keyword or omit it.

Color output function
Color output function

3. Batch Create Users

Batch user creation script
Batch user creation script

4. Verify Package Installation

Check package installed
Check package installed

5. Check Service Status

Service status check
Service status check

6. Host Liveness Detection

Three approaches are demonstrated:

Store erroneous IPs in an array and consider the host down after three ping failures.

Track failure count in a FAIL_COUNT variable.

Use a for loop that breaks on a successful ping; otherwise, report failure after the loop.

Method 1
Method 1
Method 2
Method 2
Method 3
Method 3

7. Monitor CPU, Memory, and Disk Utilization

CPU usage is examined with vmstat. Memory and disk statistics are captured via standard Linux tools, illustrated in the following images.

CPU monitoring
CPU monitoring
Memory monitoring
Memory monitoring
Disk monitoring
Disk monitoring

8. Batch Remote Disk Usage Monitoring

Prerequisite: password‑less SSH (key‑based) between the monitoring host and target machines. A configuration file lists each host as IP User Port, then a script iterates over the list to collect df output.

Remote disk monitoring config
Remote disk monitoring config

9. Website Availability Checks

Two methods are shown: a simple URL reachability test and a retry‑up‑to‑three‑times approach, mirroring the host‑liveness logic.

Check URL availability
Check URL availability
Retry URL check
Retry URL check

These examples illustrate how to write clear, maintainable Bash scripts for common system‑administration tasks, emphasizing best practices such as proper shebang usage, consistent naming, thorough commenting, and incremental testing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationBashsystem-monitoringShell scripting
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.