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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:43:42+00:00 2026-06-18T18:43:42+00:00

I have a bash script start.sh which looks like this: for thing in foo

  • 0

I have a bash script start.sh which looks like this:

for thing in foo bar; do
    {
        background_processor $thing
        cleanup_on_exit $thing
    } &
done

This does what I want: I run start.sh, it exits with code 0, and the two subshells run in the background. Each subshell runs background_processor, and when that exits, it runs cleanup_on_exit. This works even if I exit the terminal from which I originally ran start.sh (even if that was an ssh connection).

Then I tried this:

ssh user@host "start.sh"

This works, except that after start.sh has exited, ssh apparently also waits for the subshells to exit. I don’t really understand why. Once start.sh exits, the subshells become children of pid 1, and they are not even assigned with a tty… so I can’t understand how they are still associated with my ssh connection.

I later tried this:

ssh -t user@host "start.sh"

Now the processes have an assigned pseudo-tty. Now, I find that ssh does exit as soon as start.sh exits, but it also kills the child processes.

I guessed that the child processes were being sent SIGHUP in the latter case, so I did this:

ssh -t user@host "nohup start.sh"

That actually works! So, I have a solution to my practical problem, but I would like to grasp the subtleties of the SIGHUP/tty stuff here.

In summary, my questions are:

  1. Why does ssh (without -t) wait for the child processes even after start.sh exits, even though they have parent pid 1?
  2. Why does ssh (with -t) kill the child processes, apparently with a SIGHUP, even though that does not happen when I run them from a terminal and log out of that terminal?
  • 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-18T18:43:44+00:00Added an answer on June 18, 2026 at 6:43 pm

    I think I can explain this now! I had to learn a bit about what sessions and process groups are, which I did by reading The TTY Demystified.

    1. Why does ssh (without -t) wait for the child processes even after start.sh exits, even though they have parent pid 1?

    Because with no tty, ssh connects to stdin/stdout/stderr of the shell process via pipes (which are then inherited by the children), and the version of OpenSSH that I am using (OpenSSH_4.3p2) waits for those sockets to close before exiting. Some earlier versions of OpenSSH did not behave that way. There is a good explanation of this, with rationale, here.

    Conversely, when using an interactive login (or ssh -t), ssh and the processes are using a TTY and so there are no pipes to wait for.

    I can recover the behaviour I want by redirecting the streams. This variant returns immediately: ssh user@host "start.sh < /dev/null > /dev/null 2>&1"

    1. Why does ssh (with -t) kill the child processes, apparently with a SIGHUP, even though that does not happen when I run them from a terminal and log out of that terminal?

    Because bash is starting in non-interactive mode, which means that job control is disabled by default, and consequently the child processes are in the same process group as the parent bash process (which is the session leader). When the parent bash process exits, the kernel sends SIGHUP to its process group (which is in the foreground) as described in setpgid(2):

    If a session has a controlling terminal, … [and] the session leader exits, the SIGHUP signal will be sent to each process in the foreground process group of the controlling terminal.

    Conversely, when using an interactive login, bash is in interactive mode which means that job control is enabled by default, and so the child processes go into a separate process group and never receive the SIGHUP when I exit.

    I can recover the behaviour I want by using set -m to enable job control in bash. If I add set -m to start.sh, the children are no longer killed when ssh exits.

    Mysteries solved 🙂

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

Sidebar

Related Questions

I have a bash script file which starts with a function definition, like this:
I have a bash script, that I run like this via the command line:
I have a bash script which I want to start with arguments by an
I have an init.d script that looks like: #!/bin/bash # chkconfig 345 85 60
I have a bash script that has this function in it: function start_vi() {
I have a bash script which need to execute some php scripts and to
I have a bash script mystuff containing a line like lynx -dump http://example.com >tmpfile
I have a shell script run.sh which is executing a jar file. #!/bin/bash $JAVA_HOME/bin/java
I have a multi module maven project which looks something like: main component_one subcomponent_bob
Closely related to this question: Bash printf prefix I have the following Bash script

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.