Operations 17 min read

Essential Bash Scripts for MySQL Monitoring, File Sync, User Management & Deployments

This article provides a curated collection of Bash scripts that cover MySQL master‑slave replication monitoring, directory change detection with rsync, bulk user creation, website health checks, remote command execution across hosts, one‑click LNMP setup, server resource usage reporting, high‑CPU process identification, and automated deployment of Java (Tomcat) and PHP projects.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
Essential Bash Scripts for MySQL Monitoring, File Sync, User Management & Deployments

Monitor MySQL Master‑Slave Replication Status

#!/bin/bash
HOST=localhost
USER=root
PASSWD=123.com
IO_SQL_STATUS=$(mysql -h$HOST -u$USER -p$PASSWD -e 'show slave status\G' 2>/dev/null |awk '/Slave_.*_Running:/{print $1$2}')
for i in $IO_SQL_STATUS; do
    THREAD_STATUS_NAME=${i%:*}
    THREAD_STATUS=${i#*:}
    if [ "$THREAD_STATUS" != "Yes" ]; then
        echo "Error: MySQL Master‑Slave $THREAD_STATUS_NAME status is $THREAD_STATUS!" | mail -s "Master‑Slave Status" [email protected]
    fi
done

Directory Change Monitoring and Real‑Time Sync

#!/bin/bash
MON_DIR=/opt
inotifywait -mqr --format %f -e create $MON_DIR | \
while read files; do
    rsync -avz /opt /tmp/opt
    echo "$(date +'%F %T') create $files" | mail -s "dir monitor" [email protected]
done

Bulk Create 100 Users and Set Passwords

#!/bin/bash
DATE=$@
USER_FILE=user.txt
for USER in $USER_LIST; do
    if ! id $USER >/dev/null; then
        PASS=$(echo $RANDOM | md5sum | cut -c 1-8)
        useradd $USER
        echo $PASS | passwd --stdin $USER >/dev/null
        echo "$USER   $PASS" >> $USER_FILE
        echo "$USER User create successful."
    else
        echo "$USER User already exists!"
    fi
done

Batch Check Website Availability

#!/bin/bash
URL_LIST="www.baidu.com www.ctnrs.com"
for URL in $URL_LIST; do
    FAIL_COUNT=0
    for ((i=1;i<=3;i++)); do
        HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL)
        if [ $HTTP_CODE -eq 200 ]; then
            echo "$URL OK"
            break
        else
            echo "$URL retry $FAIL_COUNT"
            let FAIL_COUNT++
        fi
    done
    if [ $FAIL_COUNT -eq 3 ]; then
        echo "Warning: $URL Access failure!"
    fi
done

Remote Command Execution on Multiple Hosts

#!/bin/bash
COMMAND=$*
HOST_INFO=host.info
for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
    USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
    PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
    PASS=$(awk -v ip=$IP 'ip==$1{print $4}' $HOST_INFO)
    expect -c "
        spawn ssh -p $PORT $USER@$IP
        expect {
            \"(yes/no)\" {send \"yes\r\"; exp_continue}
            \"password:\" {send \"$PASS\r\"; exp_continue}
            \"$USER@*\" {send \"$COMMAND\r exit\r\"; exp_continue}
        }
    "
    echo "-------------------"
done

One‑Click LNMP Platform Deployment

#!/bin/bash
NGINX_V=1.15.6
PHP_V=5.6.36
TMP_DIR=/tmp
INSTALL_DIR=/usr/local
PWD_C=$PWD

echo -e "\tMenu
"
echo -e "1. Install Nginx"
echo -e "2. Install PHP"
echo -e "3. Install MySQL"
echo -e "4. Deploy LNMP"
echo -e "9. Quit"

function command_status_check() {
    if [ $? -ne 0 ]; then
        echo $1
        exit
    fi
}

function install_nginx() {
    cd $TMP_DIR
    yum install -y gcc gcc-c++ make openssl-devel pcre-devel wget
    wget http://nginx.org/download/nginx-${NGINX_V}.tar.gz
    tar zxf nginx-${NGINX_V}.tar.gz
    cd nginx-${NGINX_V}
    ./configure --prefix=$INSTALL_DIR/nginx \
        --with-http_ssl_module \
        --with-http_stub_status_module \
        --with-stream
    command_status_check "Nginx - platform check failed!"
    make -j4
    command_status_check "Nginx - compile failed!"
    make install
    command_status_check "Nginx - install failed!"
    mkdir -p $INSTALL_DIR/nginx/conf/vhost
    cp -rf $PWD_C/nginx.conf $INSTALL_DIR/nginx/conf
    rm -rf $INSTALL_DIR/nginx/html/*
    echo "ok" > $INSTALL_DIR/nginx/html/status.html
    echo '<?php echo "ok"?>' > $INSTALL_DIR/nginx/html/status.php
    $INSTALL_DIR/nginx/sbin/nginx
    command_status_check "Nginx - start failed!"
}

function install_php() {
    cd $TMP_DIR
    yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
        libcurl-devel libjpeg-devel libpng-devel openssl-devel \
        libmcrypt-devel libxslt-devel libtidy-devel
    wget http://php.net/distributions/php-${PHP_V}.tar.gz
    tar zxf php-${PHP_V}.tar.gz
    cd php-${PHP_V}
    ./configure --prefix=$INSTALL_DIR/php \
        --with-config-file-path=$INSTALL_DIR/php/etc \
        --enable-fpm --enable-opcache \
        --with-mysql --with-mysqli --with-pdo-mysql \
        --with-openssl --with-zlib --with-curl --with-gd \
        --with-jpeg-dir --with-png-dir --with-freetype-dir \
        --enable-mbstring --enable-hash
    command_status_check "PHP - platform check failed!"
    make -j4
    command_status_check "PHP - compile failed!"
    make install
    command_status_check "PHP - install failed!"
    cp php.ini-production $INSTALL_DIR/php/etc/php.ini
    cp sapi/fpm/php-fpm.conf $INSTALL_DIR/php/etc/php-fpm.conf
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm
    /etc/init.d/php-fpm start
    command_status_check "PHP - start failed!"
}

read -p "Enter number: " number
case $number in
    1) install_nginx;;
    2) install_php;;
    3) install_mysql;;
    4) install_nginx; install_php;;
    9) exit;;
esac

One‑Click Server Resource Utilization Check

#!/bin/bash
function cpu() {
    NUM=1
    while [ $NUM -le 3 ]; do
        util=$(vmstat | awk '{if(NR==3)print 100-$15"%"}')
        iowait=$(vmstat | awk '{if(NR==3)print $16"%"}')
        echo "CPU - Utilization: $util , IO Wait: $iowait"
        let NUM++
        sleep 1
    done
}

function memory() {
    total=$(free -m | awk '{if(NR==2)printf "%.1f", $2/1024}')
    used=$(free -m | awk '{if(NR==2) printf "%.1f", ($2-$NF)/1024}')
    available=$(free -m | awk '{if(NR==2) printf "%.1f", $NF/1024}')
    echo "Memory - Total: ${total}G , Used: ${used}G , Free: ${available}G"
}

function disk() {
    fs=$(df -h | awk '/^\/dev/{print $1}')
    for p in $fs; do
        mounted=$(df -h | awk "$1==\"$p\"{print $NF}")
        size=$(df -h | awk "$1==\"$p\"{print $2}")
        used=$(df -h | awk "$1==\"$p\"{print $3}")
        used_percent=$(df -h | awk "$1==\"$p\"{print $5}")
        echo "Disk - Mount: $mounted , Size: $size , Used: $used , Usage: $used_percent"
    done
}

function tcp_status() {
    summary=$(ss -antp | awk '{status[$1]++} END {for(i in status) printf i":"status[i]" "}')
    echo "TCP connections - $summary"
}

cpu
memory
disk
tcp_status

Find High CPU/Memory Processes

ps -eo user,pid,pcpu,pmem,args --sort=-pcpu | head -n 10

ps -eo user,pid,pcpu,pmem,args --sort=-pmem | head -n 10

Automated Java (Tomcat) Project Deployment

#!/bin/bash
DATE=$(date +%F_%T)
TOMCAT_NAME=$1
TOMCAT_DIR=/usr/local/$TOMCAT_NAME
ROOT=$TOMCAT_DIR/webapps/ROOT
BACKUP_DIR=/data/backup
WORK_DIR=/tmp
PROJECT_NAME=tomcat-java-demo

cd $WORK_DIR
if [ ! -d $PROJECT_NAME ]; then
    git clone https://github.com/lizhenliang/tomcat-java-demo
    cd $PROJECT_NAME
else
    cd $PROJECT_NAME
    git pull
fi

mvn clean package -Dmaven.test.skip=true
if [ $? -ne 0 ]; then
    echo "maven build failure!"
    exit 1
fi

TOMCAT_PID=$(ps -ef | grep "$TOMCAT_NAME" | egrep -v "grep|$$" | awk 'NR==1{print $2}')
[ -n "$TOMCAT_PID" ] && kill -9 $TOMCAT_PID
[ -d $ROOT ] && mv $ROOT $BACKUP_DIR/${TOMCAT_NAME}_ROOT$DATE
unzip $WORK_DIR/$PROJECT_NAME/target/*.war -d $ROOT
$TOMCAT_DIR/bin/startup.sh

Automated PHP Project Deployment

#!/bin/bash
DATE=$(date +%F_%T)
WWWROOT=/usr/local/nginx/html/$1
BACKUP_DIR=/data/backup
WORK_DIR=/tmp
PROJECT_NAME=php-demo

cd $WORK_DIR
if [ ! -d $PROJECT_NAME ]; then
    git clone https://github.com/lizhenliang/php-demo
    cd $PROJECT_NAME
else
    cd $PROJECT_NAME
    git pull
fi

if [ ! -d $WWWROOT ]; then
    mkdir -p $WWWROOT
    rsync -avz --exclude=.git $WORK_DIR/$PROJECT_NAME/* $WWWROOT
else
    rsync -avz --exclude=.git $WORK_DIR/$PROJECT_NAME/* $WWWROOT
fi
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.

automationLinuxBashscripts
Linux Cloud Computing Practice
Written by

Linux Cloud Computing Practice

Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!

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.