Practical Shell Script Automation Tips

Introduction Shell scripts are the most common automation tool for operations engineers. This post records several practical patterns used in the field. Log Rotation Cleanup #!/bin/bash # clean_logs.sh - Log cleanup script LOG_DIR="/var/log/app" KEEP_DAYS=30 find "$LOG_DIR" -name "*.log" -type f -mtime +${KEEP_DAYS} -exec gzip {} \; find "$LOG_DIR" -name "*.gz" -type f -mtime +${KEEP_DAYS} -delete echo "$(date): Log cleanup complete, kept ${KEEP_DAYS} days" Run daily via crontab: 0 2 * * * /opt/scripts/clean_logs....

January 11, 2024 · 2 mins · 369 words · XuBaojin

Production-Grade Bash Scripting Guide

Overview Bash is the most widely used scripting language in the ops world — nearly every Linux server ships with the interpreter, requiring no runtime installation. But Bash is also the language most prone to producing scripts that “work but can’t be trusted”: no type checking, errors are silent by default, variables get word-split without quotes, and pipeline failures get swallowed. The root cause of many production incidents is a Bash script that didn’t handle errors properly....

December 27, 2023 · 22 mins · 4514 words · XuBaojin