1

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

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.

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3437245
  • 347
  • 1
  • 3
  • 14

1 Answers1

2

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

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 ... ?

markp-fuso
  • 28,790
  • 4
  • 16
  • 36