CentOS Shell Template
[ root@stock.data.news.100 pts/1 2021-01-04/31@1 19:09:32 /Server/datas/downloads/scripts ]
# cat ~/test.sh
#!/bin/bash
VERSION=2.0.1
Help()
{
cat <<-EOF
# synopsis: bash $0
# description:
# date: 2020-11-03
# version: ${VERSION} [ 增脚本传参,多进程,等各项改进 ]
# author: LBC
# -- 使用介绍
# 单进程(默认在脚本所在的目录搜索${0%.*}.cnf、${0%.*}.fn;若没有配置则按脚本默认的方式执行。)
# bash $0 lock_file='sample1.lf' config_file='file1.cnf' skincare_file='file1.fn'
# 参数项
# lock_file 文本锁,用于区别不同进程
# config_file 配置文件,用于修改脚本的全局变量
# skincare_file 功能文件,用于重新脚本的函数功能
EOF
}
# 全局变量初始化
export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin'
export PATH="/ProgramFiles/redis_slink/bin:/ProgramFiles/mysql_slink/bin:${PATH}"
WORKING_DIRECTORY=$(dirname $0)
LOCK_FILE="/tmp/$(basename $0).lf"
FILE_NAME=$(basename ${0%.*})
CONFIG_FILE="${WORKING_DIRECTORY}/${FILE_NAME}.cnf"
SKINCARE_FILE="${WORKING_DIRECTORY}/${FILE_NAME}.fn"
LOG_ON=1
LOCK_ON=1
# 日志输出
Log()
{
if [ ${LOG_ON:-0} -eq 1 ] ; then
echo "$1 $2"
fi
}
# [ 基础功能 ] 文件锁,防止脚本在执行期间重复执行.
# $1=锁文件路径,$2=[0|1],0:检查锁文件是否存在,1:创建或删除锁文件
LockFile()
{
if [ ${LOCK_ON:-0} -eq 0 ] ; then
return 0
fi
local lock_file=$1
if [ ${2:-0} -eq 0 ] ; then
if [ -e ${lock_file} ] ; then
Log " " "error: $(stat -c %y ${lock_file}) script is already running."
Log ' ' "repair: Stop the process($(cat ${lock_file})) and delete the file(${lock_file})"
exit
fi
else
if [ -e ${lock_file} ] ; then
rm -f ${lock_file}
Log ' ' 'lock file is deleted'
else
echo $$ > ${lock_file}
Log ' ' "lock file is completed"
fi
fi
}
# [ 基础功能 ] 接收脚本参数
ReceiveParameters()
{
Log "base function:" "receive parameters"
#while [ "${1}" != "${1#*=}" ] ; do
while [ 0 -ne $# ] ; do
case ${1,,} in
lock_file=?*)
LOCK_FILE=${1#*=}
shift
;;
config_file=?*)
CONFIG_FILE=${1#*=}
shift
;;
skincare_file=?*)
SKINCARE_FILE=${1#*=}
shift
;;
*)
Help
exit
;;
esac
done
Log " " "lock file = ${LOCK_FILE}"
Log " " "config file = ${CONFIG_FILE}"
Log " " "skincare file = ${SKINCARE_FILE}"
}
# [ 基础功能 ] 初始化函数包含基础功能函数
Initialize()
{
Log "base function:" "Initialize"
Log ' ' 'check script is already running .'
LockFile ${LOCK_FILE}
Log ' ' 'creating lock file .'
LockFile ${LOCK_FILE} 1
sleep 120
}
Main()
{
echo "code "
Log 'unlock file .'
LockFile ${LOCK_FILE} 1
Log "$(date +%F\ %H:%M:%S)|finish"
}
Log "$(date +%F\ %H:%M:%S) bash ${WORKING_DIRECTORY}/$(basename $0) process is $$"
Log ' ' "current directory: $(pwd)"
ReceiveParameters $@
Initialize
Main
执行效果
[ root@stock.data.news.100 pts/0 2021-01-04/31@1 19:09:35 ~ ]
# bash test.sh
2021-01-04 19:09:37 bash ./test.sh process is 3272
current directory: /root
base function: receive parameters
lock file = /tmp/test.sh.lf
config file = ./test.cnf
skincare file = ./test.fn
base function: Initialize
check script is already running .
creating lock file .
lock file is completed
code
unlock file .
lock file is deleted
2021-01-04 19:11:37|finish
重复执行时的效果(一个会话先执行,另一个会话再执行)
[ root@stock.data.news.100 pts/1 2021-01-04/31@1 19:10:36 /Server/datas/downloads/scripts ]
# bash ~/test.sh
2021-01-04 19:10:39 bash /root/test.sh process is 3281
current directory: /Server/datas/downloads/scripts
base function: receive parameters
lock file = /tmp/test.sh.lf
config file = /root/test.cnf
skincare file = /root/test.fn
base function: Initialize
check script is already running .
error: 2021-01-04 19:09:37.569190148 +0800 script is already running.
repair: Stop the process(3272) and delete the file(/tmp/test.sh.lf)
posted on 2021-01-04 18:16 3L·BoNuo·Lotus 阅读(45) 评论(0) 编辑 收藏 举报