Operations 5 min read

Essential Bash Scripting Tips & Practical Linux Monitoring Scripts

This guide presents essential Bash scripting best practices and a collection of practical Linux monitoring scripts, covering topics such as random string generation, colored output functions, batch user creation, package and service checks, host ping, CPU/memory/disk utilization monitoring, remote disk checks, and website availability testing.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Essential Bash Scripting Tips & Practical Linux Monitoring Scripts

Precautions

1) Add interpreter line: #!/bin/bash

2) Use four spaces for indentation and add comments.

3) Naming conventions: uppercase for constants, lowercase for local variables, lowercase for functions, names reflect purpose.

4) Variables are global by default; use local inside functions to limit scope.

5) Two debugging commands: set -e to exit on non-zero status, set -x to print execution.

6) Test scripts before production.

1. Generate Random String or Number

Example to get a random 8‑character string:

Example to get a random 8‑digit number:

Use cksum to print CRC checksum and byte count.

2. Define a Color Output Function

Define a function using the function keyword (optional).

3. Batch Create Users

4. Check If a Package Is Installed

5. Check Service Status

6. Check Host Liveness

Method 1: Put erroneous IPs in an array and consider ping failed after three attempts.

Method 2: Store failure count in FAIL_COUNT variable and exit after three failures.

Method 3: Use a for loop to break on 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: SSH password‑less login or key‑based login between monitoring and target hosts.

Create a configuration file listing each host’s IP, user, and port.

9. Check Website Availability

1) Verify URL reachability.

2) Test URL three times, similar to host liveness check.

These shell script examples are practical and often appear in interviews; practice writing them yourself rather than just copying.

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.

SysadminBashShell scriptingLinux monitoring
MaGe Linux Operations
Written by

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.

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.