Source

woocode / sa-auto / sh / mfork / lst2run.sh

Full commit
#!/bin/bash

PROCESS_MAX=4

cmd=$1
lstdir=$2


test -z "$1" && test -z "$2" && echo "Usage: $0 COMMAND DIRECTORY" && exit 1                                                                                     

test -z "$3" && PROCESS_MAX=4 || PROCESS_MAX=$3

PROCESS=$PROCESS_MAX

for file in $lstdir/*
do
   if [ -f "$file" ]; then
     filename=$(basename $file)
     nohup $cmd $file > /apps/logs/down_${filename}.log 2>&1 &
     echo $!
     PROCESS=$(($PROCESS - 1))
     if [ $PROCESS -eq 0 ]; then
        echo "Waiting for process to finish"
        PROCESS=$PROCESS_MAX
        wait
     fi 
   fi 
done

wait


#把 $$ 换成 $! 就能获得后台job的pid
#$$是当前进程的PID
#if [ $# -eq 0 ]
#then
#        echo "Syntax: $(basename $0) string"
#        exit 1
#fi