Score:3

Exiting the terminal vs Closing the terminal

br flag

Context:-

I am running a process in bash with & appended i.e in background mode. The process has a while(1) i.e it will run forever. My bash is parent and my process becomes the child. I want to learn how Kernel handles the child when parent is terminated.

Observations:-

  1. If I exit the terminal (using exit command from the terminal), /lib/systemd/systemd --user becomes the parent process of the child
  2. If I kill the terminal process (using kill) from another terminal, same result as above
  3. If I close the terminal using cross icon, child process gets terminated too

Question:-

How is closing different from killing/exiting?

sudodus avatar
jp flag
Which version (20.04.x LTS?) and which flavour (Ubuntu Desktop, Kubuntu, Lubuntu ... Xubuntu?) are you running? Which terminal emulator are you running (gnome-terminal or ... ?) What child process is it (is it a compiled program, a shellscript ...; is it running in its own window or in the background or in the original terminal window)?
Saad Abdul Majid avatar
br flag
@sudodus, my mistake for the lack of information. Here: Ubuntu Desktop, 18.04.5 LTS, gnome-terminal, compiled program running in the terminal's window
Score:1
us flag
shk

There are different kind of signals in linux. When you exit terminal, the signal sent is different from the signal which is sent when you close terminal by clicking on x. Try going through termination signals to find how closing and exiting terminal is different from each other.

Zanna avatar
kr flag
I went through it, and I still don't know what signal is sent when we close the terminal emulator window, or what signal is sent when we type `exit` in the terminal. Can you please elaborate and explain what signals are sent and the difference between them?
Score:0
jp flag

I used ping in a gnome-terminal window and I can confirm your observations. I think it is problematic, that a process can be left running without anywhere to communicate. I will try to interpret what I notice (but I do not claim that I understand why, maybe to allow programs with an own GUI to work without nohup after exit from bash, when started in a terminal window).

  • When I exit I am exiting from bash running in the terminal window, and ping continues to run, but has nowhere to write.

  • When I kill the process, I kill the bash process in the terminal window. I did not find an explicit gnome-terminal process to kill, because the process /usr/lib/gnome-terminal/gnome-terminal-server runs them all. I am not sure that this is what you were doing, but it is what I guess.

  • When I close the window (with the x button) I exit from the window process (which in turn forces its child processes to stop including bash and ping). This is an obvious difference from the two previous cases.


When I do the corresponding exercise with xterm, there will be a separate process with the name 'xterm'.

  • When I exit I am exiting from bash running in the terminal window, and ping continues to run, but has nowhere to write.

  • When I kill the process, I kill the xterm process of the terminal window, (which in turn forces its child processes to stop, including bash and ping).

  • When I close the window (with the x button) I exit from the window process (which in turn forces its child processes to stop, including bash and ping).


You know already, that you can use nohup (together with &) if you want the child process to continue after closing the terminal window, where child process was started.


Edit 1: Final comment:

From man bash

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

We have noticed that there is no signal to stop the subshell and the process started in/from that subshell, when bash is exited, at least not when run in a terminal window. But the calling terminal window is closed. I think, that after bash is exited, the terminal window no longer knows of the subshell and the process started in/from that subshell, and therefore it cannot stop them.

Edit 2: I asked for help at Unix & Linux - Please read the answers.

Score:0
pe flag

The process launched like this ./myprocess & are child of the parent bash process opened by the terminal program. The sign & wont help you to close the terminal window before the end of a long process.

What you are looking for is the command nohup

user@host:~$ nohup bash -c '/path/to/myprocess'

More info : http://manpages.ubuntu.com/manpages/impish/en/man1/nohup.1.html
Some Examples: https://linuxhint.com/nohup_command_linux/

Saad Abdul Majid avatar
br flag
I only need to know the reason behind my observations. To make sense of what I am observing. I think it is about the signal that is dispatched to the processes.
pe flag
@Saad Abdul Majid :see first part of my answer, closing the terminal window kills the parent bash process
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.