伴随着梦想前行
22/10
2019

shell脚本实现的多进程管理

#mgr.sh
#!/bin/bash

base_dir=$(cd dirname "$0" && pwd)

TASK_COUNT=24
MUTEX_FILE=${base_dir}/count.mutex
BIN=/usr/bin/php

function maybe_wait() {
    while :
    do
        cnt=lsof ${MUTEX_FILE} | awk '$1=="sh"' | wc -l
        echo "LIMIT_TASK_COUNT: $TASK_COUNT, CURRENT: $cnt"
        if (( $cnt < $TASK_COUNT )); then
            break
        fi
        sleep 10
    done
}

for day in cat ${base_dir}/days.list
do
    echo "wait: $day, $hour"
    maybe_wait

    sh ${base_dir}/task.sh ${day} &> ${base_dir}/logs/${day}.log &
done
#task.sh
#!/bin/bash

base_dir=$(cd dirname "$0" && pwd)

MUTEX_FILE=
exec 7<>${MUTEX_FILE} 

if (( 1 > $# )); then
    echo $0 yyyy-mm-dd
    exit
fi

day=$1

echo $day

添加新评论