LinuxShell
监控 MySQL 主从同步状态是否异常脚本
#!/bin/bashHOST=localhostUSER=rootPASSWD=123.comIO_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; doTHREAD_STATUS_NAME=${i%:*}THREAD_STATUS=${i#*:}if [ "$THREAD_STATUS" != "Yes" ]; thenecho "Error: MySQL Master-Slave $THREAD_STATUS_NAME status is $THREAD_STATUS!" |mail -s "Master-Slave Staus" [url=mailto:xxx@163.com]xxx@163.com[/url]fidone
目录文件变化监控和实时文件同步
#!/bin/bashMON_DIR=/optinotifywait -mqr --format %f -e create $MON_DIR |\while read files; dorsync -avz /opt /tmp/opt#echo "$(date +'%F %T') create $files" | mail -s "dir monitor" [url=mailto:xxx@163.com]xxx@163.com[/url]done
批量创建 100 用户并设置密码脚本
#!/bin/bashDATE=$@USER_FILE=user.txtfor USER in $USER_LIST; doif ! id $USER &>/dev/null; thenPASS=$(echo $RANDOM |md5sum |cut -c 1-8)useradd $USERecho $PASS |passwd --stdin $USER &>/dev/nullecho "$USER $PASS" >> $USER_FILEecho "$USER User create successful."elseecho "$USER User already exists!"fidone
批量检测网站是否异常脚本
#!/bin/bashURL_LIST="www.baidu.com [url=http://www.ctnrs.com]www.ctnrs.com[/url]"for URL in $URL_LIST; doFAIL_COUNT=0for ((i=1;i<=3;i++)); doHTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL)if [ $HTTP_CODE -eq 200 ]; thenecho "$URL OK"breakelseecho "$URL retry $FAIL_COUNT"let FAIL_COUNT++fidoneif [ $FAIL_COUNT -eq 3 ]; thenecho "Warning: $URL Access failure!"fidone
批量主机远程执行命令脚本
#!/bin/bashCOMMAND=$*HOST_INFO=host.infofor IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); doUSER=$(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@$IPexpect {\"(yes/no)\" {send \"yes\r\"; exp_continue}\"password:\" {send \"$PASS\r\"; exp_continue}\"$USER@*\" {send \"$COMMAND\r exit\r\"; exp_continue}}"echo "-------------------"done
一键部署 LNMP 网站平台脚本
#!/bin/bashNGINX_V=1.15.6PHP_V=5.6.36TMP_DIR=/tmpINSTALL_DIR=/usr/localPWD_C=$PWDechoecho -e "\tMenu\n"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 ]; thenecho $1exitfi}function install_nginx() {cd $TMP_DIRyum install -y gcc gcc-c++ make openssl-devel pcre-devel wgetwget [url=http://nginx.org/download/nginx-]http://nginx.org/download/nginx-[/url]${NGINX_V}.tar.gztar zxf nginx-${NGINX_V}.tar.gzcd nginx-${NGINX_V}./configure --prefix=$INSTALL_DIR/nginx \--with-http_ssl_module \--with-http_stub_status_module \--with-streamcommand_status_check "Nginx - 平台环境检查失败!"make -j 4command_status_check "Nginx - 编译失败!"make installcommand_status_check "Nginx - 安装失败!"mkdir -p $INSTALL_DIR/nginx/conf/vhostalias cp=cp ; cp -rf $PWD_C/nginx.conf $INSTALL_DIR/nginx/confrm -rf $INSTALL_DIR/nginx/html/*echo "ok" > $INSTALL_DIR/nginx/html/status.htmlecho '<?php echo "ok"?>' > $INSTALL_DIR/nginx/html/status.php$INSTALL_DIR/nginx/sbin/nginxcommand_status_check "Nginx - 启动失败!"}function install_php() {cd $TMP_DIRyum install -y gcc gcc-c++ make gd-devel libxml2-devel \libcurl-devel libjpeg-devel libpng-devel openssl-devel \libmcrypt-devel libxslt-devel libtidy-develwget [url=http://docs.php.net/distributions/php-]http://docs.php.net/distributions/php-[/url]${PHP_V}.tar.gztar zxf php-${PHP_V}.tar.gzcd 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-hashcommand_status_check "PHP - 平台环境检查失败!"make -j 4command_status_check "PHP - 编译失败!"make installcommand_status_check "PHP - 安装失败!"cp php.ini-production $INSTALL_DIR/php/etc/php.inicp sapi/fpm/php-fpm.conf $INSTALL_DIR/php/etc/php-fpm.confcp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmchmod +x /etc/init.d/php-fpm/etc/init.d/php-fpm startcommand_status_check "PHP - 启动失败!"}read -p "请输入编号:" numbercase $number in1)install_nginx;;2)install_php;;3)install_mysql;;4)install_nginxinstall_php;;9)exit;;esac
一键查看服务器资源利用率
#!/bin/bashfunction cpu() {NUM=1while [ $NUM -le 3 ]; doutil=`vmstat |awk '{if(NR==3)print 100-$15"%"}'`user=`vmstat |awk '{if(NR==3)print $13"%"}'`sys=`vmstat |awk '{if(NR==3)print $14"%"}'`iowait=`vmstat |awk '{if(NR==3)print $16"%"}'`echo "CPU - 使用率: $util , 等待磁盘IO响应使用率: $iowait"let NUM++sleep 1done}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 "内存 - 总大小: ${total}G , 使用: ${used}G , 剩余: ${available}G"}function disk() {fs=$(df -h |awk '/^\/dev/{print $1}')for p in $fs; domounted=$(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 "硬盘 - 挂载点: $mounted , 总大小: $size , 使用: $used , 使用率: $used_percent"done}function tcp_status() {summary=$(ss -antp |awk '{status[$1]++}END{for(i in status) printf i":"status[i]" "}')echo "TCP连接状态 - $summary"}cpumemorydisktcp_status
找出占用 CPU 内存过高的进程脚本
ps -eo user,pid,pcpu,pmem,args --sort=-pcpu |head -n 10ps -eo user,pid,pcpu,pmem,args --sort=-pmem |head -n 10
自动发布 Java 项目(Tomcat)
#!/bin/bashDATE=$(date +%F_%T)TOMCAT_NAME=$1TOMCAT_DIR=/usr/local/$TOMCAT_NAMEROOT=$TOMCAT_DIR/webapps/ROOTBACKUP_DIR=/data/backupWORK_DIR=/tmpPROJECT_NAME=tomcat-java-demo# 拉取代码cd $WORK_DIRif [ ! -d $PROJECT_NAME ]; thengit clone [url=https://github.com/lizhenliang/tomcat-java-demo]https://github.com/lizhenliang/tomcat-java-demo[/url]cd $PROJECT_NAMEelsecd $PROJECT_NAMEgit pullfi# 构建mvn clean package -Dmaven.test.skip=trueif [ $? -ne 0 ]; thenecho "maven build failure!"exit 1fi# 部署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$DATEunzip $WORK_DIR/$PROJECT_NAME/target/*.war -d $ROOT$TOMCAT_DIR/bin/startup.sh
自动发布 PHP 项目脚本
#!/bin/bashDATE=$(date +%F_%T)WWWROOT=/usr/local/nginx/html/$1BACKUP_DIR=/data/backupWORK_DIR=/tmpPROJECT_NAME=php-demo# 拉取代码cd $WORK_DIRif [ ! -d $PROJECT_NAME ]; thengit clone [url=https://github.com/lizhenliang/php-demo]https://github.com/lizhenliang/php-demo[/url]cd $PROJECT_NAMEelsecd $PROJECT_NAMEgit pullfi# 部署if [ ! -d $WWWROOT ]; thenmkdir -p $WWWROOTrsync -avz --exclude=.git $WORK_DIR/$PROJECT_NAME/* $WWWROOTelsersync -avz --exclude=.git $WORK_DIR/$PROJECT_NAME/* $WWWROOTfi
