Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
  1. #1
    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!!!!

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    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.

  3. #3
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...