0

When this bash script is run backgrounded, it terminates the calling shell. Why is this?

#!/bin/bash
#
# weird job control, background termination of parent shell problem
set -eum
echo "Do not go gentle into that good night"
sleep 1

Save this as wat, chmod +x wat, run it as wat - no problem. Run it backgrounded:

wat &

It closes my current shell!? And even more strangely, if that last sleep line is commented out, it can run backgrounded quite happily. Can anyone explain?

I understand that it's to do with the -m (job control) option to set but I don't understand why...

6
  • cannot reproduce. what bash version is yours?
    – user313992
    Commented Aug 23, 2019 at 11:34
  • 4.4 on MacOS GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0)
    – jaygooby
    Commented Aug 23, 2019 at 11:35
  • Can reproduce on Ubuntu GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
    – jaygooby
    Commented Aug 23, 2019 at 11:42
  • 1
    I found this: stackoverflow.com/questions/18605166/… that maybe can help you, but I personally cannot reproduce. Commented Aug 23, 2019 at 11:58
  • Thanks Luciano, that sounds like exactly the same problem. I'll try and see what's odd about my bash config
    – jaygooby
    Commented Aug 23, 2019 at 12:16

2 Answers 2

1

I can see that happen if the script is run with Bash 4.3.30, but not with Bash 4.4.12. (The version of the shell running the script doesn't seem to matter that much.)

I didn't bisect that any further, but there's this in the changelog that looks like it might apply:

This document details the changes between this version, bash-4.4-rc1, and
the previous version, bash-4.4-beta.

i.  Fixed a bug that caused background processes run from non-interactive shells
    with job control enabled to place the terminal in the wrong process group
    under certain circumstances.
1
  • I'm just writing some extra debug code to see if the PGID changes depending on the invocation; should be able to see it in action
    – jaygooby
    Commented Aug 23, 2019 at 13:16
1

From man:

-m Monitor mode.

Job control is enabled. This option is on by default for interactive shells on systems that support it (see JOB CONTROL above). Background processes run in a separate process group and a line containing their exit status is printed upon their completion.

from secure log:

Received disconnect from X.X.X.X: 0

Seems, if we set to -m, the exit status is prints on a current shell after completion of a background process which is exit 0. Able to witness in securelog.

2
  • So are you saying that the exit status printed into the current shell, causes that shell to exit?
    – jaygooby
    Commented Aug 23, 2019 at 12:58
  • Secure log confirms the same
    – Siva
    Commented Aug 23, 2019 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .