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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:48:56+00:00 2026-06-10T08:48:56+00:00

using bash in linux is it possible to spawn parallel processes in the foreground?

  • 0

using bash in linux is it possible to spawn parallel processes in the foreground? For example the following :

top.sh

#!/bin/bash

./myscript1.sh &
./myscript2.sh &

will spawn two processes in parallel as background threads. However is it possible to spawn these as foreground threads? The aim is to automatically kill myscript1.sh and myscript2.sh, when top.sh is killed. Thanks

  • 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-10T08:48:58+00:00Added an answer on June 10, 2026 at 8:48 am

    You can only have one job in the foreground. You can make your script react to any signal that reaches it and forward the signal to other jobs. You need to make sure your script sticks around, if you want to have a central way of killing the subprocesses: call wait so that your script will not exit until all the jobs have died or the script itself is killed.

    #!/bin/bash
    jobs=
    trap 'kill -HUP $jobs' INT TERM HUP
    myscript1.sh & jobs="$jobs $!"
    myscript2.sh & jobs="$jobs $!"
    wait
    

    You can still kill only the wrapper script by sending it a signal that it doesn’t catch, such as SIGQUIT (which I purposefully left out) or SIGKILL (which can’t be caught).

    There’s a way to have all the processes in the foreground: connect them through pipes. Ignore SIGPIPE so that the death of a process doesn’t kill the previous one. Save and restore stdin and stdout through other file descriptors if you need them. This way the script and the background tasks will be in the same process group, so pressing Ctrl+C will kill both the wrapper script and the subprocesses. If you kill the wrapper script directly, that won’t affect the subprocesses; you can kill the process group instead by passing the negative of the PID of the wrapper script (e.g. kill -TERM -1234).

    trap '' PIPE
    {
      myscript1.sh <&3 >&4 |
      myscript2.sh <&3 >&4
    } 3<&0 4>&1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using bash with linux to accomplish adding content to the top of
Can someone post a simple example of using named pipes in Bash on Linux?
I'm using objcopy on bash (ubuntu linux) and im trying to copy 2 sections
In a bash script in Linux, I am using flock [the command flock, not
Possible Duplicate: using bash: write bit representation of integer to file I need to
I'm trying to convert a series of bytes from hex to bin using bash.
Bash 4 on Linux ~ I have an array of possible values. I must
I need to make a radiolist in bash script using dialog interface, for example
I am using bash shell on linux and want to use more than 10
How we run php script using Linux bash? php file test.php test.php contains: <?php

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.