Background Process Shell Scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Background Process Shell Scripting
# 1  
Old 04-12-2008
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 01:08 PM..
# 2  
Old 04-12-2008
Code:
ls x* | head -10 >input.dat

# 3  
Old 04-12-2008
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.
# 4  
Old 04-12-2008
Thanks Jean. I think it will work for me Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting errors for ftp process

Hi i am facing problem in shell scripting for ftp process getting following errors here is the script & result vi GtpTxnlogs_ftp.sh "GtpTxnlogs_ftp.sh" 40 lines, 921 characters #!/usr/bin/bash ###################################################################################### #... (4 Replies)
Discussion started by: Sarmistha
4 Replies

2. Shell Programming and Scripting

Shell scripting issue-running the background script

I have written the below query to genrate a telephone.I am passing account number from oracle database. I am calling 2 scripts which generate the bill 1. bip.sh (it runs in the background) 2.runXitInvoice_PROFORMA_integ bip.sh generates a number which runXitInvoice_PROFORMA_integ uses.How... (7 Replies)
Discussion started by: rafa_fed2
7 Replies

3. Shell Programming and Scripting

How to run multiple functions in Background in UNIX Shell Scripting?

Hi, I am using ksh , i have requirement to run 4 functions in background , 4 functions call are available in a case that case is also in function, i need to execute 1st function it should run in background and return to case and next i will call 2nd function it should run in background and... (8 Replies)
Discussion started by: karthikram
8 Replies

4. Shell Programming and Scripting

How to stop the process in shell scripting?

Hi all, I have tried the below code to execute. #! /bin/bash date1=`date -d "today 08:00:00" +%s` date2=`date -d "today 08:01:00" +%s` path=/home/user01/red/IDC/sample cd $path java Cspsamp 111.19.5.172 7025 rd1 rd1 "5022=Query|5026=109378|4=627|5=E:VD|5042=$date1|5049=$date2"... (5 Replies)
Discussion started by: aish11
5 Replies

5. Shell Programming and Scripting

Running Shell Script in the cron, background process

Hi, i was looking for an answer for some trouble im having runing a script in the cron, thing is, that when i run it manually it works just fine. But when cron runs it, it just doenst work. I saw a reply on a similar subject, suggesting that the . .profile worked for you, but im kind of... (9 Replies)
Discussion started by: blacksteel1988
9 Replies

6. Shell Programming and Scripting

Creating background process for my shell

I actually posted this problem on a different forum, but figured this would be a more appropriate place to post it. OK so I've created my own shell, but I can't get the background process function to run properly! What I want to do is to run a process in the background, and also print when the... (2 Replies)
Discussion started by: hansel13
2 Replies

7. Shell Programming and Scripting

Shell Scripting not showing in process when it goes to sleep

Hi All, Here is my script: sleep_time=`echo "9.6 * $num_servers"|bc| cut -d. -f1` if ; then sleep_time=3600;fi ### Allow the compare script to kick in after 1 hour at the least logger "Sleeping for $sleep_time seconds ...Will call compare.sh thereon" $act_log sleep $sleep_time #sleep... (3 Replies)
Discussion started by: ntgobinath
3 Replies

8. UNIX for Dummies Questions & Answers

Wait Process in Shell Scripting.

Hello, I have a script that needs to wait till the previous process is done within the same script.But my script doesnt wait till that it is done.Can anyone suggest how I can stop my process till the previous task is done. I tried 'wait' but I dont have a static process id so is there... (2 Replies)
Discussion started by: sud
2 Replies

9. Shell Programming and Scripting

facing problem in starting a process in background using shell script.

hey all, i am working on sun solaris machine and i want to start a process in background using shell script (actually i wanna start tomcat server using shell script). please dont tell me that append a & at last because this is not working in the shell script. i have also used nohup and... (8 Replies)
Discussion started by: dtomar
8 Replies

10. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies
Login or Register to Ask a Question