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.
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.
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.
3. Batch Create Users
4. Verify Package Installation
5. Check Service Status
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.
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.
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.
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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
