Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:32:24+00:00 2026-05-23T14:32:24+00:00

I have a large set of files for which some heavy processing needs to

  • 0

I have a large set of files for which some heavy processing needs to be done.
This processing in single threaded, uses a few hundred MiB of RAM (on the machine used to start the job) and takes a few minutes to run.
My current usecase is to start a hadoop job on the input data, but I’ve had this same problem in other cases before.

In order to fully utilize the available CPU power I want to be able to run several those tasks in paralell.

However a very simple example shell script like this will trash the system performance due to excessive load and swapping:

find . -type f | while read name ; 
do 
   some_heavy_processing_command ${name} &
done

So what I want is essentially similar to what “gmake -j4” does.

I know bash supports the “wait” command but that only waits untill all child processes have completed. In the past I’ve created scripting that does a ‘ps’ command and then grep the child processes out by name (yes, i know … ugly).

What is the simplest/cleanest/best solution to do what I want?


Edit: Thanks to Frederik: Yes indeed this is a duplicate of How to limit number of threads/sub-processes used in a function in bash
The “xargs –max-procs=4” works like a charm.
(So I voted to close my own question)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:32:25+00:00Added an answer on May 23, 2026 at 2:32 pm
    #! /usr/bin/env bash
    
    set -o monitor 
    # means: run background processes in a separate processes...
    trap add_next_job CHLD 
    # execute add_next_job when we receive a child complete signal
    
    todo_array=($(find . -type f)) # places output into an array
    
    index=0
    max_jobs=2
    
    function add_next_job {
        # if still jobs to do then add one
        if [[ $index -lt ${#todo_array[*]} ]]
        # apparently stackoverflow doesn't like bash syntax
        # the hash in the if is not a comment - rather it's bash awkward way of getting its length
        then
            echo adding job ${todo_array[$index]}
            do_job ${todo_array[$index]} & 
            # replace the line above with the command you want
            index=$(($index+1))
        fi
    }
    
    function do_job {
        echo "starting job $1"
        sleep 2
    }
    
    # add initial set of jobs
    while [[ $index -lt $max_jobs ]]
    do
        add_next_job
    done
    
    # wait for all jobs to complete
    wait
    echo "done"
    

    Having said that Fredrik makes the excellent point that xargs does exactly what you want…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large set of files, some of which contain special characters in
I have a large set of XML files of a propriatary schema -the XML
I have a large set off files (hdf) that I need to enable search
I have a python module which generates large data files which I want to
I've got some very large XML files which I read using a System.Xml.Serialization.XmlSerializer .
I have a reasonably large (thousands of files) project which is divided between client
I have an application which needs to define indexes in various .h files in
I have some huge binary files which I'm currently reading using memory mapping on
I have an ordered list which uses large Georgia for the numbering and smaller
I have a large set of data (a data cube of 250,000 X 1,000

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.