How to Control Number of Processes Running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Control Number of Processes Running
# 1  
Old 05-13-2010
How to Control Number of Processes Running

Hi

Is there a way to count how many processes a script has started, count how many of these have finished, and make the script wait if their difference goes over a given threshold?

I am using a script to repeatedly execute a code (~100x) which converts 2 data files into one .plt which is in the format for Tecplot. These files are large, 300-1000MB.
The code reads an input file which is altered before each instance of the code is executed. I've done this by hand (never again), and could get about 5-6 instances going at a time (ie the 1st process would finish before I could start the 7th). (The input is read at the very beginning so I can alter it while code is running.)
My problem is once I scripted this process, MANY instances can get going at once, and the computer gets bogged down. I inserted a 'sleep' command, but this isn't robust enough, and depending on other loads on the computer the same problem can happen.

see script below

Thanks!
drbones
Code:
 1 #!/usr/bin/env tcsh
  2 foreach name (file_a_01*0)
  3 echo $name
  4
  5 set number=`echo $name | sed s/file_a_//`
  6 echo $number
  7
  8 set scrfile=`echo scr | sed s/r/r$number/`
  9 sed '2 s/[0-9]*/'$number'/' <code.in >new
 10 mv new code.in
 11 ./code.exe>$scrfile&
 12 sleep 20
 13 end

# 2  
Old 05-13-2010
Hi.

You could use wait.

Quote:
(csh man page) The wait builtin command causes the shell to wait for all background jobs to complete.
i.e.
Code:
% date; sleep 20 &; sleep 5 &; wait; date
[1] 29009
[2] 29010
Fri 14 May 2010 01:41:51 CEST
Fri 14 May 2010 01:42:11 CEST
[2]  + Done                          sleep 5
[1]  + Done                          ( date; sleep 20 )

(in a good shell (i.e. not csh), you have better options for job control!)
# 3  
Old 05-13-2010
if your script is sufficiently self-aware...in a different shell than the one you've shown...you can go ahead and supply a self-referencing function to grep the ps for itself, to get a count and/or check for file opens (fuser) on the dependent files.

This was actually raised just recently too...as was the deprecated status of the C shell.

HTH
# 4  
Old 05-13-2010
Thanks for the quick replies.

yes yes, I've heard all the bashing of csh (no pun intended), unfortunately I've been encouraged by my advisor (i'm a in a phd program) to use tcsh. I've only started using scripts over the past week, and instantly see their value. However, I've very new to unix and am not familiar enough with functions/terminology for the arguments against csh to be very convincing to me.

As for the wait command. I believe I could figure out how to start a few instances, but would have to wait for all of these to complete, before starting another batch. I'd like to get 5 going, then start the sixth as soon as the first finishes, etc. Am I misinterpreting the functionality of 'wait'?

As for grepping 'ps' this was something I had thought about, or even checking out 'uptime' (i'm working with multiple cores, and the code is parallel). I'm pretty sure I could somehow use grepped data to control a loop, which would be like a dynamic sleep command, but was looking for something more direct, this seems like a work-around.

Any other suggestions/advice?

curleb, you mentioned this was brought up recently. Could you direct me to the thread?

Thanks again
drbones
# 5  
Old 05-13-2010
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Many processes running at the same time

Hello everybody , I launched cron to execute a task every hour but the job takes more than hour that's why I'm getting more than 1000 cron processes running at the same time !!! My question is how to tell cron not to execute unless the job terminated in order to have only one process running .... (14 Replies)
Discussion started by: beautymind
14 Replies

2. UNIX for Dummies Questions & Answers

How do I find the number of processes running on root?

Is there a certain man command I'm missing here? I searched in ps but I couldn't find something that would give me the number of processes running on root. I only want to see the number of processes, not the processes itself. (2 Replies)
Discussion started by: l3monz
2 Replies

3. Linux

Running processes

Hi guys is it normal to have 5-10 cron/syslog processes running... in my case i got 10 cron process running. (4 Replies)
Discussion started by: batas
4 Replies

4. Solaris

Running processes on GZ/LZ

Hi guys just a question is it normal to see running process on a non-global zone in the global zone... processes such as cron. (3 Replies)
Discussion started by: batas
3 Replies

5. Shell Programming and Scripting

how to know the running processes.

Hi can anybody help me regarding this.. i want know the output of ps -ef with explanation. how can we know the running processess. this is the output of ps -elf F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD 19 T root 0 0 0 0 SY ... (1 Reply)
Discussion started by: rajesh_pola
1 Replies

6. UNIX for Dummies Questions & Answers

How do you print the number of processes that each user is currently running in Unix?

Ok, so I know there's a way to do this, but I've been trying to find out all afternoon with no luck. I think it should print out something like this: 1 bin 2 daemon 6 duo Where the numbers on the left are the number of processes being run by the user whose name is listed on the right. Is... (4 Replies)
Discussion started by: Duo11
4 Replies

7. Shell Programming and Scripting

Help in running two processes in parellel

I have a script something like this: #!/usr/bin/ksh CLASSPATH=/apps/opt/db2udb/admin/db2bdt/sqllib/java/db2java.zip:/apps/opt/db2udb/admin/db2bdt/sqllib/java/db2jcc.jar:/apps/opt/db2udb/admin/db2bdt/sqllib/function:$CLASSPATH export CLASSPATH ... (7 Replies)
Discussion started by: ss3944
7 Replies

8. Shell Programming and Scripting

Need help with running processes script

I'm doing a script with the Shell. I need that it only show the number of running processes. Ex: echo "There are `command` running processes" Thnx! Pd: Sorry the idiom. I'm spanish. (2 Replies)
Discussion started by: Ikebana
2 Replies

9. Shell Programming and Scripting

Keep a certain number of background processes running

I've got a bit of code I'm trying to work on... What i want to happen is ... at all times have four parallel mysql dump and imports running. I found the follow code snippet on the forum and modified it to work by starting four concurrent processes but it waits until all four are done before... (7 Replies)
Discussion started by: dgob123
7 Replies

10. Shell Programming and Scripting

Determine Number of Processes Running

I am pretty new to unix, and I have a project to do. Part of the project asks me to determine the number of processes running and assign it to a variable. I know how to every part of the project but determine the number of processes running. How can I get just the number of processes... (4 Replies)
Discussion started by: wayne1411
4 Replies
Login or Register to Ask a Question