Operations 8 min read

7 Practical Bash Scripts to Boost Linux Sysadmin Efficiency

This article presents seven hands‑on Bash script examples that cover concurrent hostname collection, process state counting, bulk file renaming, automated jstack logging, GC log analysis, high‑traffic IP detection with conditional reporting, and moving large files while sorting them by size, each with complete code snippets.

Liangxu Linux
Liangxu Linux
Liangxu Linux
7 Practical Bash Scripts to Boost Linux Sysadmin Efficiency

Task 1 – Concurrent hostname collection and fastest CPU info

The script runs in parallel on a list of hosts, measures the time each hostname command takes, records the duration to hostname.txt, waits for all jobs, then selects the host with the smallest elapsed time and displays its CPU usage via top -b -n 1.

#!/bin/bash
ALL_HOSTS=(IP_ADDRESS IP_ADDRESS)
for host in ${ALL_HOSTS[*]}; do
  {
    start_time=$(date +'%s')
    ssh $host "hostname" >/dev/null
    sleep 2
    stop_time=$(date +'%s')
    time_consuming=$((stop_time-start_time))
    echo "$host: $time_consuming" >>hostname.txt
  } &
 done
wait
host=$(sort -n -k2 hostname.txt | head -1 | awk -F':' '{print $1}')
ssh $host "top -b -n 1"

Task 2 – Count Linux processes by state and clean zombies

This script enumerates numeric entries under /proc, reads each process's Status line, tallies running, stopped, sleeping, and zombie processes, writes zombie PIDs to zombie.txt, kills them, and finally prints a summary.

#!/bin/bash
ALL_PROCESS=$(ls /proc/ | egrep '[0-9]+')
running_count=0
stoped_count=0
sleeping_count=0
zombie_count=0
for pid in ${ALL_PROCESS[*]}; do
  test -f /proc/$pid/status && state=$(egrep "State" /proc/$pid/status | awk '{print $2}')
  case "$state" in
    R) running_count=$((running_count+1)) ;;
    T) stoped_count=$((stoped_count+1)) ;;
    S) sleeping_count=$((sleeping_count+1)) ;;
    Z) zombie_count=$((zombie_count+1))
       echo "$pid" >>zombie.txt
       kill -9 "$pid"
       ;;
  esac
done
echo -e "total: $((running_count+stoped_count+sleeping_count+zombie_count))
running: $running_count
stoped: $stoped_count
sleeping: $sleeping_count
zombie: $zombie_count"

Task 3 – Rename ".sh" files to ".shell" and delete the second line

The script finds all ".sh" files recursively, renames each to the same base name with a ".shell" extension, then removes the second line of the new file using sed.

#!/bin/bash
ALL_SH_FILE=$(find . -type f -name "*.sh")
for file in ${ALL_SH_FILE[*]}; do
  filename=$(echo $file | awk -F'.sh' '{print $1}')
  new_filename="${filename}.shell"
  mv "$file" "$new_filename"
  sed -i '2d' "$new_filename"
done

Task 4 – Manage /tmp/jstack directory and periodic jstack capture

The script ensures /tmp/jstack exists (or empties it), then enters an infinite loop that sleeps one hour, captures a jstack dump of a process named inceptor, stores it with a timestamped filename, and keeps only the newest ten files by deleting the oldest when the count exceeds ten.

#!/bin/bash
DIRPATH='/tmp/jstack'
CURRENT_TIME=$(date +'%F-%H:%M:%S')
if [ ! -d "$DIRPATH" ]; then
  mkdir "$DIRPATH"
else
  rm -rf "$DIRPATH"/*
fi
cd "$DIRPATH"
while true; do
  sleep 3600
  # replace 'inceptor' with the actual Java process name
  pid=$(ps -ef | grep 'inceptor' | grep -v grep | awk '{print $2}')
  jstack $pid >> "jstack_${CURRENT_TIME}"
  dir_count=$(ls | wc -l)
  if [ "$dir_count" -gt 10 ]; then
    rm -f $(ls -tr | head -1)
  fi
done

Task 5 – Extract today’s GC logs and compute average and maximum durations

Two awk pipelines read a Hive server log, isolate the second column (time), strip colons, then calculate the average GC time and the maximum GC duration, appending the results to capture_hive_log.log.

#!/bin/bash
awk '{print $2}' hive-server2.log | tr -d ':' | awk '{sum+=$1} END {print "avg: ", sum/NR}' >>capture_hive_log.log
awk '{print $2}' hive-server2.log | tr -d ':' | awk '{max=0} {if ($1+0 > max+0) max=$1} END {print "Max: ", max}' >>capture_hive_log.log

Task 6 – Detect top 20 IPs on port 80 and conditionally generate a system activity report

The script repeatedly checks the 20th‑largest request count among IPs contacting port 80; if that count exceeds 500, it runs sar -A and writes the output to alert.txt. Otherwise it sleeps six seconds and retries until the condition is met.

#!/bin/bash
state="true"
while $state; do
  SMALL_REQUESTS=$(netstat -ant | awk -F'[ :]+' '/:22/{count[$4]++} END {for(ip in count) print count[ip]}' | sort -n | head -20 | head -1)
  if [ "$SMALL_REQUESTS" -gt 500 ]; then
    sar -A > alert.txt
    state="false"
  else
    sleep 6
    continue
  fi
done

Task 7 – Move files larger than 10 KB to /tmp and list them by size descending

The script finds files larger than 10 KB in the current directory, moves them to /tmp, then lists the files in /tmp sorted by size from largest to smallest, printing only the filenames.

#!/bin/bash
DIRPATH='/tmp'
FILEPATH='.'
find "$FILEPATH" -size +10k -type f | xargs -I {} mv {} "$DIRPATH"
ls -lS "$DIRPATH" | awk '{if(NR>1) print $NF}'

These seven Bash examples demonstrate common sysadmin automation tasks, providing ready‑to‑run code that can be adapted to real‑world environments to improve productivity and reliability.

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.

ShellSysadminScriptingBash
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.