1. 程式人生 > >shell 指令碼例項--持續更新

shell 指令碼例項--持續更新

1、keepalived檢測nginx指令碼

#!/bin/bash
# author:gan_ke
# this scripts is used for keepalived to check nginx status

A=`ps -C nginx --no-header |wc -l`   #檢視當前是否有nginx 程序     
if [ $A -eq 0 ];then                            
      /usr/local/nginx/sbin/nginx                #重啟nginx
      if [ `ps -C nginx --no-header |wc -l
` -eq 0 ];then #nginx重啟失敗,則停掉keepalived服務,進行VIP轉移 killall keepalived #強制kill關於keepalived的程序(實現高可用) fi fi

2、每天定時備份http的訪問日誌

#!/bin/sh
# author:gan_ke
# this scripts  backup http access_log everyday

cd /var/log/httpd/

##備份日誌
/bin/mv access_log access_$(date +%F).log

##過載服務(平滑,不影響業務)
/etc/init.d/httpd reload

這是指令碼,完成後還要加入定時任務:

30 5 * * * /bin/sh /scripts/cut_http_log.sh

3、檢查系統是否裝某個軟體,沒有則安裝

#!/bin/bash
# author: gan_ke
# this script is check the service on your system,if no,then install it

# 使用命令檢視是否有包安裝上
res=`rpm -qa $1 | wc -l`
if  [ $res -eq  0 ] ; then
    yum install -y $1
else echo "$1 is already installed." fi

4、在指定目錄下,通過5個隨機字母+ganke建立10個txt檔案

#!/bin/bash
# author:gan_ke
# this script is touch file with random name

#先判斷目錄是否存在,若不存在則建立目錄
[ -d /root/test ] || mkdir /root/test -p

cd /root/test

for i in `seq 10`
do
    touch `echo $RANDOM|md5sum|cut -c 1-5|tr "[0-9]" "[a-z]"`_ganke.txt  
done