分享40個常用shell腳本源碼,請關注,將有更多shell腳本分享

需要腳本源碼的同學,請先轉發後關注,私信“HelloShell”獲取下載地址

shell腳本在日常運維工作中,起到相當大的作用,對於自動化運維來說省工省力。

以下是本人工作中積累的shell腳本大全,如下:


分享40個常用shell腳本源碼,請關注,將有更多shell腳本分享


分享40個常用shell腳本源碼,請關注,將有更多shell腳本分享


apache、mysql、nginx腳本系列

1、mysql系列

1.1、判斷mysql運行狀態

#!/bin/bash

#author:andychen

MYSQL=/etc/init.d/mysqld

LogPath=/tmp/mysql.log

portNum=`netstat -ntpl | grep 3306 | wc -l`

mysqlProcessNum=`ps -ef | grep mysqld |grep -v grep |wc -l`

if [ $portNum -eq 1 ] && [ $mysqlProcessNum -eq 2 ];then

echo "mysql is running"

else

$MYSQL restart >>$LogPath

sleep 10

portNum=`netstat -ntpl | grep 3306 | wc -l`

mysqlProcessNum=`ps -ef | grep mysqld |grep -v grep |wc -l`

if [ $portNum -ne 1 ] && [ $mysqlProcessNum -ne 2 ];then

while true

do

killall mysqld >dev/null 2>&1

[ $? -ne 0 ] && break

sleep 1

done

$MYSQL restart >>$LogPath && status="successfully" || status="failure"

mail -s "mysql startup status is $status" [email protected]

fi

fi

1.2、mysql啟動腳本

cat mysqld.sh

#!/bin/bash

# mysql啟動腳本

. /etc/init.d/functions

path="/etc/init.d/mysqld"

function usage(){

echo "$0 {start|stop|restart}"

exit 1

}

[ $# -ne 1 ] && usage

function_mysql_start(){

$path start &>/dev/null

if [ $? -eq 0 ]

then

sleep 5

action "start mysql" /bin/true

else

action "start mysql" /bin/false

fi

}

function_mysql_stop(){

$path stop &>/dev/null

if [ $? -eq 0 ]

then

action "stop mysql" /bin/true

else

action "stop mysql" /bin/false

fi

}

function_mysql_restart(){

function_mysql_stop

sleep 3

function_mysql_start

}

case $1 in

start)

function_mysql_start

;;

stop)

function_mysql_stop

;;

restart)

function_mysql_restart

;;

*)

printf "Usage:$0 {start|stop|restart}\\n"

esac

2、apache系列

2.1、通過偵聽端口、進程、遠程判斷是否正常

例一:

cat judge_http_port.sh

#!/bin/bash

#通過偵聽httpd端口來判斷

port=`netstat -ntpl | grep 80|awk -F '[: ]+' '{print $4}'`

if [ "$port" == "80" ];then

echo "httpd is running."

else

echo "httpd is not running."

/etc/init.d/httpd start

fi

例二:

cat judge_http_port2.sh

#!/bin/bash

#通過偵聽httpd端口個數來判斷

HttpPortNum=`netstat -ntpl | grep 80|wc -l`

if [ $HttpPortNum -eq 1 ];then

echo "httpd is running."

else

echo "httpd is not running."

/etc/init.d/httpd start

fi

例三:

cat judge_http_port3.sh

#!/bin/bash

#通過遠程端口來判斷

HttpPortNum=`nmap 192.168.0.13 -p 80 | grep open|wc -l`

if [ $HttpPortNum -eq 1 ];then

echo "httpd is running."

else

echo "httpd is not running."

/etc/init.d/httpd start

fi

2.2、Apache啟動腳本

cat httpd.sh

#!/bin/bash

httpd="/usr/sbin/apachectl"

# Source function library. #加載函數庫

. /etc/init.d/functions

case "$1" in

start)

$httpd start >& /dev/null

[ $? -eq 0 ] && action "httpd is started" /bin/true||\\

action "httpd is started" /bin/false

;;

stop)

$httpd stop >& /dev/null

[ $? -eq 0 ] && action "httpd is stopped" /bin/true||\\

action "httpd is stopped" /bin/false

;;

restart)

$httpd restart >& /dev/null

[ $? -eq 0 ] && action "httpd is restarted" /bin/true||\\

action "httpd is restarted" /bin/false

;;

*)

echo "Useage:$0 {start|stop|restart}"

exit

;;

esac

3、nginx系列

判斷nginx運行狀態

例一:

cat judge_http_port.sh

#!/bin/bash

HttpPortNum=`nmap 192.168.0.13 -p 80 | grep open|wc -l`

if [ $HttpPortNum -eq 1 ];then

echo "nginx is running."

else

echo "nginx is not running."

/etc/init.d/nginx start

fi

例二:

cat judge_http_port2.sh

#!/bin/bash

#wget -T 10 -q --spider http://192.168.0.13 >&/dev/null

curl -s http://192.168.0.13 >&/dev/null

if [ $? -eq 0 ];then

echo "nginx is running."

else

echo "nginx is not running."

/etc/init.d/nginx start

fi

例三:

cat judge_http_port3.sh

#!/bin/bash

httpCode=`curl -I -s http://192.168.0.13 | head -1 | awk '{print $2}'`

if [ "$httpCode" == "200" ];then

echo "nginx is running."

else

echo "nginx is not running."

/etc/init.d/nginx start

fi

例四:

cat judge_http_port4.sh

#!/bin/bash

[ -f /etc/init.d/functions ] && . /etc/init.d/functions||exit 1

httpCode=`curl -I -s http://192.168.0.13 | head -1 | awk '{print $2}'`

if [ "$httpCode" == "200" ];then

action "nginx is running." /bin/true

else

action "nginx is not running." /bin/false

/etc/init.d/nginx start && \\

action "nginx is started." /bin/true

fi

例五:

cat judge_http_port5.sh

#!/bin/bash

if [ $# -ne 2 ];then

echo "Usage:$0 ip port"

exit

fi

HttpPortNum=`nmap $1 -p $2|grep open|wc -l`

if [ $HttpPortNum -eq 1 ];then

echo "$1 $2 is open."

else

echo "$1 $2 is closed."

fi

測試:

sh judge_http_port.sh baidu.com 80


分享到:


相關文章: