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 6850045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:04:33+00:00 2026-05-27T01:04:33+00:00

I have three shell scripts say A, B and C. I need to run

  • 0

I have three shell scripts say A, B and C. I need to run A in the background and run B in the background till A finishes its execution in the background. Similarly run C in the foreground till A and B finish their execution.

I was doing this for 2 processes earlier in this way.

./A.sh &  
while ps -p $! >/dev/null; do   
./B.sh  
done 

I need to run B in background and C in foreground till A finishes its execution in background. How do I modify the above code.

  • 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-27T01:04:34+00:00Added an answer on May 27, 2026 at 1:04 am

    This will run A & B in the background with C in the foreground; B&C will loop until A finishes:

    #!/bin/bash
    
    ./A.sh &
    APID=$!
    
    while ps -p ${APID} >/dev/null; do
        ./B.sh & ./C.sh
    done
    

    Example from my box:

    [ 09:39 jon@hozbox.com ~/SO/bash ]$ cat A.sh
    #!/bin/bash
    echo "A Started at: `date`"
    sleep 30
    echo "A Finished at: `date`"
    
    [ 09:39 jon@hozbox.com ~/SO/bash ]$ cat B.sh
    #!/bin/bash
    echo "B Started at: `date`"
    sleep 10
    echo "B Finished at: `date`"
    
    [ 09:39 jon@hozbox.com ~/SO/bash ]$ cat C.sh
    #!/bin/bash
    echo "C Started at: `date`"
    sleep 5
    echo "C Finished at: `date`"
    
    [ 09:38 jon@hozbox.com ~/SO/bash ]$ ./how-to-program-in-this-bash-script-of-background-processes.sh
    A Started at: Wed Nov 23 09:38:39 PST 2011
    C Started at: Wed Nov 23 09:38:39 PST 2011
    B Started at: Wed Nov 23 09:38:39 PST 2011
    C Finished at: Wed Nov 23 09:38:44 PST 2011
    C Started at: Wed Nov 23 09:38:44 PST 2011
    B Started at: Wed Nov 23 09:38:44 PST 2011
    B Finished at: Wed Nov 23 09:38:49 PST 2011
    C Finished at: Wed Nov 23 09:38:49 PST 2011
    C Started at: Wed Nov 23 09:38:49 PST 2011
    B Started at: Wed Nov 23 09:38:49 PST 2011
    B Finished at: Wed Nov 23 09:38:54 PST 2011
    C Finished at: Wed Nov 23 09:38:54 PST 2011
    C Started at: Wed Nov 23 09:38:54 PST 2011
    B Started at: Wed Nov 23 09:38:54 PST 2011
    B Finished at: Wed Nov 23 09:38:59 PST 2011
    C Finished at: Wed Nov 23 09:38:59 PST 2011
    C Started at: Wed Nov 23 09:38:59 PST 2011
    B Started at: Wed Nov 23 09:38:59 PST 2011
    B Finished at: Wed Nov 23 09:39:04 PST 2011
    C Finished at: Wed Nov 23 09:39:04 PST 2011
    C Started at: Wed Nov 23 09:39:04 PST 2011
    B Started at: Wed Nov 23 09:39:04 PST 2011
    
    A Finished at: Wed Nov 23 09:39:09 PST 2011
    B Finished at: Wed Nov 23 09:39:09 PST 2011
    C Finished at: Wed Nov 23 09:39:09 PST 2011
    [ 09:39 jon@hozbox.com ~/SO/bash ]$
    


    What about forking A and B at the same time, but put A second so $! gives A‘s pid:

    ./B.sh & ./A.sh &
    while ps -p $! >/dev/null; do   
    ./C.sh  
    done 
    

    Heres an example from my box:

    [ 19:08 jon@hozbox.com ~ ]$ date
    Tue Nov 22 19:08:19 PST 2011
    [ 19:08 jon@hozbox.com ~ ]$ sleep 15 & sleep 20 &
    [1] 1126
    [2] 1127
    [ 19:08 jon@hozbox.com ~ ]$ while ps -p $! > /dev/null; do sleep 1 && date; done
    Tue Nov 22 19:08:26 PST 2011
    Tue Nov 22 19:08:27 PST 2011
    Tue Nov 22 19:08:28 PST 2011
    Tue Nov 22 19:08:29 PST 2011
    Tue Nov 22 19:08:31 PST 2011
    Tue Nov 22 19:08:32 PST 2011
    Tue Nov 22 19:08:33 PST 2011
    Tue Nov 22 19:08:34 PST 2011
    Tue Nov 22 19:08:35 PST 2011
    Tue Nov 22 19:08:36 PST 2011
    Tue Nov 22 19:08:37 PST 2011
    Tue Nov 22 19:08:38 PST 2011
    [1]-  Done                    sleep 15
    Tue Nov 22 19:08:39 PST 2011
    Tue Nov 22 19:08:40 PST 2011
    Tue Nov 22 19:08:41 PST 2011
    Tue Nov 22 19:08:42 PST 2011
    Tue Nov 22 19:08:43 PST 2011
    [2]+  Done                    sleep 20
    Tue Nov 22 19:08:44 PST 2011
    [ 19:08 jon@hozbox.com ~ ]$
    

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

Sidebar

Related Questions

in my repository i have several shell scripts that run in a cygwin console
I need some help from the shell-script gurus out there. I have a .txt
I have three values I need to align in a dropdown box. How can
I have to write a script to send mails using unix shell scripts. The
I have two shell scripts, one that serves as the main program and another
I've got a few Unix shell scripts where I need to check that certain
I am writhing shell script. I want three script to run in different terminal.
I have a Java Webapplication running on Tomcat, executing Shell scripts at runtime in
I have an application that I'm creating and would like to run a shell
I have three tables: page, attachment, page-attachment I have data like this: page ID

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.