如何不耍流氓的做運維之-SHELL指令碼
阿新 • • 發佈:2019-01-09
寫下我自己的模組函式,直接複製可用:
舉一個例子:
#!/bin/bash
#***************************************************************************
# *
# * @file:pid_run.sh
# * @author:Luolired
# * @date:2016-01-05 16:26
# * @version 0.5
# * @description: 節點提升為master呼叫的指令碼
# * 1.指令碼標準化 notify_master "/etc/keepalived/scripts/pid_run.sh"
# * @Copyright (c) 007ka all right reserved
# * @updatelog:
# * 1.更新程式標準化邏輯性
# * 2.優化日誌的輸出
# * 3.增加同步鎖狀態標識,用以區分同步開啟
# * 4.增加Keepalived主備切換功能開啟鎖,用以初始化啟動搶佔操作多開或Stop原有業務
#**************************************************************************/
export LANG=zh_CN.GBK
1.4.1 結果輸出函式模組
### Print error messges eg: _err "This is error"
function _err()
{
#echo -e "\033[1;31m[ERROR] [email protected]\033[0m" >&2
echo -e "\033[1;31m[ERROR] [email protected]\033[0m"
}
### Print notice messages eg: _info "This is Info"
function _info()
{
#echo -e "\033[1;32m[Info] [email protected]\033[0m" >&2
echo -e "\033[1;32m[Info] [email protected]\033[0m"
}
1.4.2 日誌輸出函式模組
###LOG_PATH
###程式執行all日誌輸出路徑
g_s_LOG_PATH=/var/applog/${G_LOG_FILE}
mkdir -p $g_s_LOG_PATH
g_s_LOGDATE=`date +"%F"`
#執行指令碼生成的日誌
g_s_LOGFILE="${g_s_LOG_PATH}/pid_run.${g_s_LOGDATE}.log"
### LOG to file eg:g_fn_LOG "Test"
g_fn_LOG()
{
s_Ddate=`date +"%F %H:%M:%S"`
echo "[$s_Ddate] $*" >> $g_s_LOGFILE
}
1.4.3 flock鎖函式模組
### Add flock Avoid more open program
function _lock()
{
flock -n 3
[ $? -eq 1 ] && { _err "Fault $(basename $0) is Already in Runing,Plesase Waiting....^_^ GoodBye!!!"; exit; }
_info "$(basename $0) Current Pid is:$$"
_log "$(basename $0) Current Pid is:$$"
sleep 50
} 3<>/tmp/$(basename $0 | awk -F. 'BEGIN{OFS="."};{NF=NF-1;print $0}').lock
function main()
{
_lock
}
#main
main
舉一個例子:
#!/bin/bash
#***************************************************************************
# *
# * @file:pid_run.sh
# * @author:Luolired
# * @date:2016-01-05 16:26
# * @version 0.5
# * @description: 節點提升為master呼叫的指令碼
# * 1.指令碼標準化 notify_master "/etc/keepalived/scripts/pid_run.sh"
# * @Copyright (c) 007ka all right reserved
# * @updatelog:
# * 1.更新程式標準化邏輯性
# * 2.優化日誌的輸出
# * 3.增加同步鎖狀態標識,用以區分同步開啟
# * 4.增加Keepalived主備切換功能開啟鎖,用以初始化啟動搶佔操作多開或Stop原有業務
#**************************************************************************/
export LANG=zh_CN.GBK
1.4.1 結果輸出函式模組
### Print error messges eg: _err "This is error"
function _err()
{
#echo -e "\033[1;31m[ERROR]
echo -e "\033[1;31m[ERROR] [email protected]\033[0m"
}
### Print notice messages eg: _info "This is Info"
function _info()
{
#echo -e "\033[1;32m[Info] [email protected]\033[0m" >&2
echo -e "\033[1;32m[Info] [email protected]\033[0m"
}
1.4.2 日誌輸出函式模組
###LOG_PATH
###程式執行all日誌輸出路徑
g_s_LOG_PATH=/var/applog/${G_LOG_FILE}
mkdir -p $g_s_LOG_PATH
g_s_LOGDATE=`date +"%F"`
#執行指令碼生成的日誌
g_s_LOGFILE="${g_s_LOG_PATH}/pid_run.${g_s_LOGDATE}.log"
### LOG to file eg:g_fn_LOG "Test"
g_fn_LOG()
{
s_Ddate=`date +"%F %H:%M:%S"`
echo "[$s_Ddate] $*" >> $g_s_LOGFILE
}
1.4.3 flock鎖函式模組
### Add flock Avoid more open program
function _lock()
{
flock -n 3
[ $? -eq 1 ] && { _err "Fault $(basename $0) is Already in Runing,Plesase Waiting....^_^ GoodBye!!!"; exit; }
_info "$(basename $0) Current Pid is:$$"
_log "$(basename $0) Current Pid is:$$"
sleep 50
} 3<>/tmp/$(basename $0 | awk -F. 'BEGIN{OFS="."};{NF=NF-1;print $0}').lock
function main()
{
_lock
}
#main
main