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

  • SEARCH
  • Home
  • 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 8363207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:07:23+00:00 2026-06-09T12:07:23+00:00

I have more than 10 tasks to execute, and the system restrict that there

  • 0

I have more than 10 tasks to execute, and the system restrict that there at most 4 tasks can run at the same time.

My task can be started like:
myprog taskname

How can I write a bash shell script to run these task. The most important thing is that when one task finish, the script can start another immediately, making the running tasks count remain 4 all the time.

  • 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-06-09T12:07:24+00:00Added an answer on June 9, 2026 at 12:07 pm

    I chanced upon this thread while looking into writing my own process pool and particularly liked Brandon Horsley’s solution, though I couldn’t get the signals working right, so I took inspiration from Apache and decided to try a pre-fork model with a fifo as my job queue.

    The following function is the function that the worker processes run when forked.

    # \brief the worker function that is called when we fork off worker processes
    # \param[in] id  the worker ID
    # \param[in] job_queue  the fifo to read jobs from
    # \param[in] result_log  the temporary log file to write exit codes to
    function _job_pool_worker()
    {
        local id=$1
        local job_queue=$2
        local result_log=$3
        local line=
    
        exec 7<> ${job_queue}
        while [[ "${line}" != "${job_pool_end_of_jobs}" && -e "${job_queue}" ]]; do
            # workers block on the exclusive lock to read the job queue
            flock --exclusive 7
            read line <${job_queue}
            flock --unlock 7
            # the worker should exit if it sees the end-of-job marker or run the
            # job otherwise and save its exit code to the result log.
            if [[ "${line}" == "${job_pool_end_of_jobs}" ]]; then
                # write it one more time for the next sibling so that everyone
                # will know we are exiting.
                echo "${line}" >&7
            else
                _job_pool_echo "### _job_pool_worker-${id}: ${line}"
                # run the job
                { ${line} ; } 
                # now check the exit code and prepend "ERROR" to the result log entry
                # which we will use to count errors and then strip out later.
                local result=$?
                local status=
                if [[ "${result}" != "0" ]]; then
                    status=ERROR
                fi  
                # now write the error to the log, making sure multiple processes
                # don't trample over each other.
                exec 8<> ${result_log}
                flock --exclusive 8
                echo "${status}job_pool: exited ${result}: ${line}" >> ${result_log}
                flock --unlock 8
                exec 8>&-
                _job_pool_echo "### _job_pool_worker-${id}: exited ${result}: ${line}"
            fi  
        done
        exec 7>&-
    }
    

    You can get a copy of my solution at Github. Here’s a sample program using my implementation.

    #!/bin/bash
    
    . job_pool.sh
    
    function foobar()
    {
        # do something
        true
    }   
    
    # initialize the job pool to allow 3 parallel jobs and echo commands
    job_pool_init 3 0
    
    # run jobs
    job_pool_run sleep 1
    job_pool_run sleep 2
    job_pool_run sleep 3
    job_pool_run foobar
    job_pool_run foobar
    job_pool_run /bin/false
    
    # wait until all jobs complete before continuing
    job_pool_wait
    
    # more jobs
    job_pool_run /bin/false
    job_pool_run sleep 1
    job_pool_run sleep 2
    job_pool_run foobar
    
    # don't forget to shut down the job pool
    job_pool_shutdown
    
    # check the $job_pool_nerrors for the number of jobs that exited non-zero
    echo "job_pool_nerrors: ${job_pool_nerrors}"
    

    Hope this helps!

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

Sidebar

Related Questions

How can one have more than a Wizard control on the same page without
I have a long-running process that must run every five minutes, but more than
I have more than one Event Listener that calls a specific function, and I
Can an if statement have more than one then statements? # this works a
I have a script that I run which does a lot of tasks and
I have a fairly simple, though expensive, task that I need to run in
I have more than 100 million unique strings (VARCHAR(100) UNIQUE in MySQL database). Now
I have more than 400 000 id's in NOT IN statement. Whether it will
I have more than 5000 image links and I need to find a way
I have more than 3000 members on a site built using wordpress. I am

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.