在旧版本的Bash中运行并行进程-版本3.2 [英] Running parallel processes in older versions of Bash - version 3.2

查看:37
本文介绍了在旧版本的Bash中运行并行进程-版本3.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与此同时运行多个进程:

I am trying to run multiple process simultaneously with this:

#!/usr/bin/env bash

read -r -p "Enter number of sessions: " pro_count
read -r -p "directory of files: " d

nodeb_job() {
    printf 'Connecting to %s\n' "$i"
    cd || exit
    if [ ! -d "$d" ]; then
        mkdir "$d"/log
    fi
    foo "$i" "$d"/"$i" >  "$d"/log/"$i"
    printf 'Done with %s\n' "$i"
}

j=0
for i in $(ls "$d"); do
    if (( j++ >= pro_count )); then
        wait -n
    fi
    nodeb_job &
done
wait

它不起作用,因为我有一个较旧的Bash版本:

It is not working, because I have an older version of Bash:

user@dacc2:~$ bash --version
GNU bash, version 3.2.57(1)-release (i386-pc-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.

我是 Solaris 10上的用户,没有 GNU并行.有什么办法可以在我仍然可以从提示中指定会话数的同时?

I am a user on Solaris 10, without GNU parallel. Is there a way I can do this while still being able to specify the number of sessions from the prompt?

推荐答案

假设您不介意轮询解决方案,一种基于计数背景作业数量的简单/快速-n-dirty方法> ...

Assuming you don't mind a polling solution, one simple/quick-n-dirty approach based on counting the number of background jobs ...

j=0
for i in $(ls "$d"); do

    while [[ $(jobs | wc -l) -ge ${pro_count} ]]
    do
            sleep 2
    done

    nodeb_job &
done 
wait

将睡眠时间(在此示例中为2秒)调整为您的环境中有意义的时间.例如,如果预计 nodeb_job()调用需要花费几分钟,而您又不着急,则可以执行 sleep 30 之类的操作.另一方面,如果 nodeb_job()调用很快,并且/或者您想快速开始下一项工作,那么 sleep 1 可能是合适的...?

Adjust the sleep time (2 seconds in this example) to whatever makes sense in your env. For example, if the nodeb_job() calls are expected to take a few minutes, and you're not in a big hurry, you could do something like sleep 30. On the other hand, if the nodeb_job() calls are quick and/or you want a fast start to the next job, then perhaps sleep 1 would be appropriate ... ?

这篇关于在旧版本的Bash中运行并行进程-版本3.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆