Score:2

Bash script launching background process breaks terminal output and kills background process

bt flag

Problem

Hello! After moving some code from Ubuntu 20 to Ubuntu 22 I've realized some odd behavior from some of the bash scripts that I was launching. The first noticeable problem being that after running the script, the terminal starts to print the text following a staircase pattern as follows:

line1
     line2
          line3
               ...
                   lin
eN

Furthermore, any text that you try to write in the terminal does not show in the screen (but the commands still work). This mostly becomes a minor inconvenience as it is just visual since the commands still work as normal. The main problem is that when you run a process in the background from the same script, after the script finishes and the process is still launched, any key interaction with the terminal will instantly kill the background process.

This error only happens in Ubuntu 22

Expected behavior

The expected behavior of this code happens if your OS is Ubuntu 20. The script acts as expected and prints the numbers continuously. In order to kill the process you'll need to use a command such as "kill" or "killall". The terminal remains free to be used for future commands.

Minimal Example

After much debugging I managed to write a minimal example showcasing the error, the necessary files are the following:

File: test.cpp (compile as g++ test.cpp -o test)

// Minimal example in C++

#include <iostream>
#include <chrono>
#include <thread>

int main() {
    uint64_t i = 0;
    while (true) {
        std::cout << i++ << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    return 0;
}

File: test.py

# Minimal example in Python

import time

if __name__ == '__main__':
    i = 0
    while True :
        print(i)
        i+=1
        time.sleep(1)

File: scriptC++.sh

#!/usr/bin/bash

sudo ./test &

sleep 3

File: scriptPy.sh

#!/usr/bin/bash

sudo python3 test.py &

sleep 3

The redundancy in files is mostly to showcase that the error is not due to C++ or Python and it's something related to Ubuntu 22 or a new version of bash or some other library (bash and low level libraries are not my expertise).

Extra Information

Some extra information I managed to gather related to the problem:

  • If the background process finishes before the bash script then everything works fine. This error only happens in the case that the background process lives longer than the bash script.
  • The background process needs to be launched with "sudo" privileges. Otherwise the error does not happen.

Question

My main question would be if this is a bug in bash, Ubuntu22, or some related library, and if other people are experiencing this problem. I've launched this minimal example in two Ubuntu22 computers yielding the same results, but I also have the same libraries installed in both so I can't rule out that it might be a third party library. And finally, if it is a bug, how should I report it? I've seen the command "ubuntu-bug" but it requires me to specify a process which I'm not sure which one would be in this case.

Raffa avatar
jp flag
Related: [Gnome Terminal not displaying correctly in 22.04](https://askubuntu.com/q/1458458/968501)
Score:1
sr flag

I ran into this as well. Using sudo -b rather than & to move the root process to the background seems to fix it and is probably the more "correct" way to do this.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.