Running shell script in parallel

后端 未结 8 2042
无人及你
无人及你 2020-11-30 19:35

I have a shell script which

  1. shuffles a large text file (6 million rows and 6 columns)
  2. sorts the file based the first column
  3. outputs 1000 fi
8条回答
  •  伪装坚强ぢ
    2020-11-30 20:15

    IDLE_CPU=1
    NCPU=$(nproc)
    
    int_childs() {
        trap - INT
        while IFS=$'\n' read -r pid; do
            kill -s SIGINT -$pid
        done < <(jobs -p -r)
        kill -s SIGINT -$$
    }
    
    # cmds is array that hold commands
    # the complex thing is display which will handle all cmd output
    # and serialized it correctly
    
    trap int_childs INT
    {
        exec 2>&1
        set -m
    
        if [ $NCPU -gt $IDLE_CPU ]; then
            for cmd in "${cmds[@]}"; do
                $cmd &
                while [ $(jobs -pr |wc -l) -ge $((NCPU - IDLE_CPU)) ]; do
                    wait -n
                done
            done
            wait
    
        else
            for cmd in "${cmds[@]}"; do
                $cmd
            done
        fi
    } | display
    

提交回复
热议问题