Quantcast
Viewing all articles
Browse latest Browse all 4466

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

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               ...                   lineN

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 Pythonimport timeif __name__ == '__main__':    i = 0    while True :        print(i)        i+=1        time.sleep(1)

File: scriptC++.sh

#!/usr/bin/bashsudo ./test &sleep 3

File: scriptPy.sh

#!/usr/bin/bashsudo 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.


Viewing all articles
Browse latest Browse all 4466

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>