The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
facing problem in starting a process in background using shell script. dtomar Shell Programming and Scripting 8 04-17-2008 05:11 AM
How to include RETURN KEY with Background process "&" in Shell Script racbern Shell Programming and Scripting 1 03-11-2008 03:30 AM
How to check process/cpu utilisation thru unix shell scripting manas_ranjan UNIX for Dummies Questions & Answers 0 07-25-2007 01:00 PM
cause a process to be in background avnerht Shell Programming and Scripting 0 06-22-2004 06:29 AM
capture the process id when starting a background process jleavitt Shell Programming and Scripting 10 04-04-2002 05:04 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-12-2008
Registered User
 

Join Date: Apr 2008
Posts: 2
Background Process Shell Scripting

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 ?

Last edited by dhieraj; 04-12-2008 at 09:08 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-12-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,094
Code:
ls x* | head -10 >input.dat
Reply With Quote
  #3 (permalink)  
Old 04-12-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,199
The following script show a possible way (the SUBMIT_JOB_LIMIT is set the maximun instance count) :

Code:
#!/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 :
Code:
$ 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.
Reply With Quote
  #4 (permalink)  
Old 04-12-2008
Registered User
 

Join Date: Apr 2008
Posts: 2
Thanks Jean. I think it will work for me
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0