shell脚本规划化模板
shell腳本規劃化模板
Linux運維過程中,shell腳本是不可缺少的工具,但是每個運維人員編程的習慣都不一樣,很多時候就是實現某個功能,寫出來的腳本都是爛七八糟的。
腳本必須規范化,應該從以后幾個方面著手:
1、腳本必須記錄日志,盡可能多的把執行命令操作寫入日志當中,有便于日后可以追蹤;
2、腳本盡可能實現函數化模塊化,函數名具有可讀性;
3、腳本必須要有傳入參數;
4、腳本執行必須是單進程的,即該腳本某個時間段只能運行一次;
5、腳本執行過程中必須加鎖機制;
6、腳本注釋是不可缺少的;
7、腳本最好統一入口(main函數),個人喜好;
大致分為這7點,運維必須規范化流程化,不做流氓運維,對自己工作負責,也便于其他同事方便查看或接手,我個人不推崇腳本復雜化,搞一堆大家看不懂的。
舉例說明,上線是運維不可缺少的工作,本人測試實現一個很簡單的功能:
1、jboss容器發布java代碼(不考慮配置文件更新)
1)關閉服務;
2)拷貝代碼至指定目錄;
3)啟動服務;
2、服務器上面開好幾個jboss實例(按業務模塊化),自動識別出每個容器(目錄:/data/)
3、根據模塊化發布版本,每次只更新一個業務模塊版本
?
#!/bin/bash
#######################################################
# $Name: release.sh ?
# $Version: v1.0
# $Function: release for jboss Script?
# $Author: Verrion Jian?
# $Create Date: 2016-09-22?
# $Description: Sub module to release for firm?
#######################################################
# 定義腳本環境變量
SHELL_NAME="release.sh"
SHELL_DIR="/data/scripts"
SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"
LOCK_FILE="/tmp/${SHELL_NAME}.lock"
PACKAGE_DIR="/data/code"
# 記錄日志
log_output(){
LOG_INFO=$1
echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") : ${SHELL_NAME} : ${LOG_INFO}" >> ${SHELL_LOG}
}
# 腳本用法,自動化列出服務器上面存在的所有實例
usage(){
get_modules
str=`echo ${module_array[@]} |sed s/[[:space:]]/'|'/g`
echo $"Usage: $0 [$str]" && exit
}
# 添加鎖文件
lock(){
touch ${LOCK_FILE}
}
# 刪除鎖文件
unlock(){
rm -f ${LOCK_FILE}
}
# 判斷代碼臨時目錄和文件是否存在
is_package_exist(){
if [ ! -d $PACKAGE_DIR ];then
log_output "The script exec as insufficient --> The release directory is non-existent"
echo "The release directory is non-existent" && exit
fi
package_num=`ls $PACKAGE_DIR |wc -l`
if [ $package_num -eq 0 ];then
log_output "The script exec as insufficient --> The packages for release is empty"
echo "The packages for release is empty,please check directory:"$PACKAGE_DIR" and upload packages" && exit
fi
}
# 列出服務器上面jboss容器個數
get_container(){
container_name=`ls -l /data/ |grep jboss | awk '{print $9}'|xargs`
}
# 列出實例模塊信息,進程號等(如:test_login.war為java代碼包)
get_modules(){
get_container
for i in $container_name
do
war_num=`ls /data/$i/server/default/deploy/test*war | wc -l`
if [ $war_num -ne 1 ];then
log_output "The container deploy to not standard--> Num of the war_package can only one"
echo "The container deploy to not standard--> Num of the war_package can only one" && exit
else
war_name=`ls /data/$i/server/default/deploy/test*war | cut -d / -f7 | cut -d - -f1`
pid=`ps -ef |grep $i |grep -v grep |awk '{print $2}'`
module_name=$war_name" "$module_name
pids=$pid" "$pids
jboss_name=$i" "$jboss_name
fi
done
module_array=($module_name)
pid_array=($pids)
jboss_array=($jboss_name)
}
# 模塊化發布上線
release_module(){
# ensure a process is running at best
if [ -f "$LOCK_FILE" ];then
log_output "${SHELL_NAME} is running"
echo "${SHELL_NAME}" is running && exit
fi
# ensure the package exist
is_package_exist
# denfie varchar
packagename=$1
packagenum=$2
packagenum=`expr $packagenum - 1`
target_pid=${pid_array[$packagenum]}
target_jboss=${jboss_array[$packagenum]}
target_dir="/data/$target_jboss/server/default/deploy"
package_str=`ls $PACKAGE_DIR`
log_output "release module for platform system start"
lock
if [[ $package_str =~ $packagename ]];then
#kill -9 $target_pid
echo "kill -9 $target_pid"
log_output "kill -9 $target_pid"
#echo $target_pid
sleep 3
#rm -rf $target_dir/$packagename*
echo "rm -rf $target_dir/$packagename*"
log_output "rm -rf $target_dir/$packagename*"
#ls $target_dir/$packagename*
#cp -r $PACKAGE_DIR/$packagename* $target_dir
echo "cp -r $PACKAGE_DIR/$packagename* $target_dir"
log_output "cp -r $PACKAGE_DIR/$packagename* $target_dir"
#ls $PACKAGE_DIR/$packagename*
else
log_output "The package of publish directory is not found"
echo "The package of publish directory is not found" && exit
fi
log_output "release module for platform system stop"
unlock
}
# Main函數入口
main(){
tag=0
if [ -z $1 ];then
usage
fi
get_modules
for i in $module_name
do
let count+=1
if [ $i = $1 ];then
pkgname=$1
pkgnum=$count
release_module "$pkgname" "$pkgnum"
tag=1
break
fi
done
if [ $tag -eq 0 ];then
module_name=""
usage
fi
}
# 執行腳本
main $1
轉載于:https://www.cnblogs.com/verrion/p/shellscript_standardized.html
總結
以上是生活随笔為你收集整理的shell脚本规划化模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: # PHP - 使用PHPMailer发
- 下一篇: Spring整合Hibernate。。。