# Background Process Shell Scripting

**URL:** <https://community.unix.com/t/background-process-shell-scripting/192817>\
**Category:** Shell Programming and Scripting\
**Created:** [April 12, 2008, 11:18am UTC](https://community.unix.com/t/background-process-shell-scripting/192817 "2008-04-12T11:18:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhieraj](https://community.unix.com/letter_avatar/dhieraj/32/5_5575768a8748004e209b776fc1b2916d.png) [@dhieraj](https://community.unix.com/u/dhieraj)\
**Post date:** [April 12, 2008, 11:18am UTC](https://community.unix.com/t/background-process-shell-scripting/192817/1 "2008-04-12T11:18:51Z")

</div>

I have a following program:

echofunc()  
{  
filename=$1  
echo "reading $filename"  
while read line  
do  
echo $line;  
sleep 6;  
done \< $filename  
}

split -5 new.dat  
ls x\* \> input.dat  
while read file  
do  
echofun $file &  
done \< input.dat

====================================================================================================================================  
Here new.dat is a file which has many 300 line of records in it. So after split 60 files will be formed.

So in this program how do I limit only 10 instances of echofun() to run at a time ?

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/5_5575768a8748004e209b776fc1b2916d.png) [@era](https://community.unix.com/u/era)\
**Post date:** [April 12, 2008, 11:51am UTC](https://community.unix.com/t/background-process-shell-scripting/192817/2 "2008-04-12T11:51:55Z")

</div>

```nohighlight
ls x* | head -10 >input.dat

```

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [April 12, 2008, 1:43pm UTC](https://community.unix.com/t/background-process-shell-scripting/192817/3 "2008-04-12T13:43:41Z")

</div>

The following script show a possible way (the SUBMIT\_JOB\_LIMIT is set the maximun instance count) :

```nohighlight
#!/usr/bin/bash
#submit_jobs

the_job() {
   echo "$(date +%T) - Start of job $1"
   sleep $((RANDOM % 30))
   echo "$(date +%T) - End of job $1"
}

SUBMIT_JOB_LIMIT=5
SUBMIT_JOB_SLEEP=5

submit_job() {
   job_act=$(jobs | wc -l)
   while ((job_act >= $SUBMIT_JOB_LIMIT))
   do
      sleep $SUBMIT_JOB_SLEEP
      job_act=$(jobs | wc -l)
   done
   (( job_cnt += 1 ))
   the_job $job_cnt &
}

for ((i=1; i<=10; i++))
do
   submit_job $i
done

```

An execution example :

```nohighlight
$ submit_jobs
19:37:38 - Start of job 3
19:37:39 - Start of job 4
19:37:39 - Start of job 5
19:37:38 - Start of job 1
19:37:38 - Start of job 2
19:37:40 - End of job 1
19:37:45 - Start of job 6
19:37:47 - End of job 3
19:37:51 - Start of job 7
19:37:52 - End of job 2
19:37:55 - End of job 6
19:37:56 - Start of job 8
19:37:56 - Start of job 9
19:37:58 - End of job 4
19:37:59 - End of job 5
$ 19:38:02 - Start of job 10
19:38:06 - End of job 9
19:38:14 - End of job 8
19:38:20 - End of job 7
19:38:29 - End of job 10

```

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![dhieraj](https://community.unix.com/letter_avatar/dhieraj/32/5_5575768a8748004e209b776fc1b2916d.png) [@dhieraj](https://community.unix.com/u/dhieraj)\
**Post date:** [April 12, 2008, 2:16pm UTC](https://community.unix.com/t/background-process-shell-scripting/192817/4 "2008-04-12T14:16:49Z")

</div>

Thanks Jean. I think it will work for me 🙂
