Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page for each, while?
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 06-26-2008   #1 (permalink)
idiotboy
Just Joined!
 
Join Date: Jun 2008
Posts: 5
for each, while?

OK Gang-

I'm definitely a scripting newbie. I need some help!

Lets say I've issued a command that gave me a list of data into $nodes. I want to run a command against those items, but want to limit it to a pre-defined amount of processes. So even though the list may have 20 items, I only want 4 to run at a time.

Does this make sense?

So do I use a for each, or a while. I'm thinking a while loop with a counter, but how do I tell my script to run through each line of the $node variable?

Thanks!!!!
idiotboy is offline   Reply With Quote
Old 06-27-2008   #2 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 209
I’ve done something similar but not in Linux but a different UNIX OS (Tru64). It was to start multiple backups up to a predefined limit. The method I used was to run each instance as a child process in the background. Before I started an instance I checked how many were already running in the background with the ‘jobs’ command and if it was at the max, looped until one or more had finished. That code looks something like this:

Code:
typeset integer _ct
<start your for each loop>
_lp=”yes”
while [ “$_lp” = “yes” ]
    do
       _ct=`jobs | wc –l`
       if [ $_ct –lt 4 ]
         then _lp=”no”
         else sleep 60
         fi
    done

<start the next instance>
<end of your for each loop>
Remember to do another wait loop again after your for each loop to allow those child processes remaining time to finish.

There was discussion about backup processes in another thread that you might want to review.
http://www.linuxforums.org/forum/lin...t-command.html

Hope that helps.
vsemaska is offline   Reply With Quote
Old 06-27-2008   #3 (permalink)
idiotboy
Just Joined!
 
Join Date: Jun 2008
Posts: 5
This is awesome.. however, I guess I should have been more specific. I'm actually calling a program, and issuing commands to it. So there aren't really unix jobs that can be tracked. Hmm.... I think I just made it more complicated.

I guess I'd really have to have logic in the script to query the program to see if the "job" is still running.
idiotboy is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:31 PM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0