Linux Shell 多线程编程--原创
相关命令文件
1、runCommandInThreads.sh 多线程实现主代码
cdir=$(dirname $BASH_SOURCE) ##Current directory of this script file. source $cdir/logUtil.sh function createFile() { local dir=$1 local idx=$2 local fl="$dir/$idx" touch $fl } function execShellCommand() { local commd=$1 local subPID="$!" $(debug "[$subPID] Execute command ‘$commd‘") eval "$commd" } ### 设置并启动多线程 ### ## 参数:1、执行命令列表(对应一个数组)2、并发执行线程数 ## 命令格式: source runCommandInThreads.sh && setupThreadsImmediately [执行命令列表] [并发执行线程数] function setupThreadsImmediately() { local startTimeSecs=$(date +"%s") local parrelThreadsSize=${2:-2} local tmp=$1[@] local commands=(${!tmp}) local commandsSize=${#commands[*]} local parrelThreadPidFileStoreDir="/tmp/parrel/"`date +%Y%m%d%H%M%S%s` if [ $commandsSize -le 0 ]; then echo "No commands input." return fi if [ ! -d $parrelThreadPidFileStoreDir ]; then trace "Create direcotry:"$parrelThreadPidFileStoreDir mkdir -p $parrelThreadPidFileStoreDir fi $(info "parrelThreadsSize:${parrelThreadsSize} ,commandsSize:$commandsSize") ## # for ((i=0;i<$parrelThreadsSize;i++));do # $(createFile $parrelThreadPidFileStoreDir "$i") # done ## local mainPID="main" local threadCount=0 for ((i=0; i<commandsSize; i++));do while [ -z "" ] do local chkFl="$(checkEnableEnroll $parrelThreadPidFileStoreDir $parrelThreadsSize $mainPID)" if [ ! -z $chkFl ]; then #Start new thread. threadCount=$(($threadCount+1)) touch $chkFl $(trace "[$mainPID] Start new sub-thread") { ##let threadCount=$threadCount+1 local tidx=$(($i+1)) local subPID="$tidx-$!" local subStartTm=$(date +"%s") ##[ ! -f $chkFl ] && $(debug "[$subPID] file:$chkFl") echo "$subPID" > $chkFl ##Do process in thread. ##local cmd=() ##eval cmd=\${$commands[@]} ##cmd="$cmd" cmd="${commands[i]}" cmd=${cmd//,/ } cmd=$cmd" "$subPID ##$(execShellCommand "$cmd") $(debug "[$subPID] Execute command ‘$cmd‘") eval "$cmd" #------------Test------------- # $(debug "[$subPID] Execute command ‘$cmd‘") # local ev=`expr $tidx % 2` # if [ $ev -eq 0 ]; then # sleep 4 # else # sleep 2 # fi [ -f $chkFl ] && rm $chkFl && $(trace "[$subPID] Deleted file $chkFl") local subEndTm=$(date +"%s") $(info "$threadCount/$commandsSize [$subPID] Sub-thread has complete, elapse: "$(calcElapse $subStartTm $subEndTm)) } & ## Work in background. break; fi #chkFl="$(checkEnableEnroll $parrelThreadPidFileStoreDir $parrelThreadsSize $mainPID)" done done ##debug "Delete direcotry:"$parrelThreadPidFileStoreDir wait # Wait for all sub threads. local endTimeSecs=$(date +"%s") ##local elapseTime=$(($startTimeSecs-$endTimeSecs)) info "--- Finish all, elapse: "$(calcElapse $startTimeSecs $endTimeSecs)"-----" rm -rf $parrelThreadPidFileStoreDir } function calcElapse() { local st=$1 local et=$2 local elapseTime=$(($et-$st)) echo $((elapseTime/60))" mins "$((elapseTime%60))" seconds" } function checkEnableEnroll() { local dir=$1 local len=$2 local pid=$3 local fl="" local i=0; for ((i=0;i<$len;i++));do fl="$dir/$i" if [ ! -f $fl ]; then trace "[$pid] found "$fl echo $fl return else trace "Ignore $fl" fi done echo "" }
2、 logUtil.sh 打印日志
# if [ ! -z $INCLUDING_LOG_UTIL ]; then # return # fi export INCLUDING_LOG_UTIL="true" export LOG_DEFAULT_LVL=-1 export LOG_TRACE_LVL=10 export LOG_DEBUG_LVL=20 export LOG_INFO_LVL=30 export LOG_WARN_LVL=40 export LOG_ERROR_LVL=50 function trace() { local logLvl=${LOGLVL:-$LOG_DEFAULT_LVL} if [ $logLvl -le $LOG_TRACE_LVL ]; then local tm="`date +%T`" echo "TRACE $tm $*">&2 fi } function debug() { local logLvl=${LOGLVL:-$LOG_DEFAULT_LVL} if [ $logLvl -le $LOG_DEBUG_LVL ]; then local tm="`date +%T`" echo "DEBUG $tm $*">&2 fi } function info() { local logLvl=${LOGLVL:-$LOG_DEFAULT_LVL} if [ $logLvl -le $LOG_INFO_LVL ]; then local tm="`date +%T`" echo "INFO $tm $*">&2 fi } function warn() { local logLvl=${LOGLVL:-$LOG_DEFAULT_LVL} if [ $logLvl -le $LOG_WARN_LVL ]; then local tm="`date +%T`" echo "WARN $tm $*">&2 fi } function error() { local logLvl=${LOGLVL:-$LOG_DEFAULT_LVL} if [ $logLvl -le $LOG_ERROR_LVL ]; then local tm="`date +%T`" echo "ERROR $tm $*">&2 fi }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。