Master Essential Linux Shell Scripts for System Monitoring and Automation
This guide presents practical Bash scripting techniques—including precautions, random string generation, color output functions, bulk user creation, package checks, service status verification, host liveness testing, resource monitoring, disk usage audits, and website availability checks—to help you automate Linux system administration tasks effectively.
Precautions
1) Add interpreter at the top: #!/bin/bash
2) Use four spaces for indentation; add plenty of comments.
3) Naming conventions: uppercase for constants, lowercase for local variables, lowercase for functions, names should reflect purpose.
4) Variables are global by default; use local inside functions to limit scope.
5) Two commands help debugging: set -e exits on non‑zero status, set -x prints each command.
6) Test scripts thoroughly before deploying to production.
1. Generate Random Strings or Numbers
Generate a random 8‑character string:
Generate a random 8‑digit number:
cksumprints CRC checksum and byte count.
2. Define a Color Output Function
Use the function keyword to define a function, with or without the keyword.
3. Batch Create Users
4. Check If a Package Is Installed
5. Check Service Status
6. Verify Host Liveness
Method 1: Put erroneous IPs into an array and consider a host down after three failed pings.
Method 2: Store failure count in a FAIL_COUNT variable and treat three failures as down.
Method 3: Use a for loop that breaks on a successful ping; otherwise report failure.
7. Monitor CPU, Memory, and Disk Utilization
CPU: Use vmstat to analyze CPU statistics.
Memory:
Disk:
8. Batch Host Disk Utilization Monitoring
Prerequisite: password‑less SSH or key‑based login between monitoring and target hosts.
Create a configuration file containing host IP, user, and port.
9. Check Website Availability
1) Verify URL reachability.
2) Test URL three times; the logic mirrors host liveness checking.
These shell script examples are practical and frequently appear in interview questions; hands‑on practice is essential rather than copying and pasting code.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
