Ubuntu – Why do some programs running from Terminal using ‘&’ close when Terminal does and others do not

command lineexecute-commandsudo

I was just wondering, for instance when I launch qtox with:

qtox &

And then close Terminal, qtox closes with it. However when running etherape using:

sudo etherape &

Closing Terminal doesn't close or cause any problem to Etherape. And among different applications there is different behaviour, some close when Terminal does, others do not, how come? Why do some close when others don't? I am running Ubuntu GNOME 15.10 with GNOME 3.18.

Best Answer

When you close a terminal the terminal sends a SIGHUP signal to the shell; the shell, in turn, sends a SIGHUP signal to all its children process groups, which include backgrounded process groups;

How each single process will react to the signal is entirely up to the process: if the process didn't define a handler for the signal and tell the kernel (via some syscall such as signal() or sigaction()) that it wishes to handle it, the kernel executes the default handler for the signal, which in case of a SIGHUP signal consists in terminating the process.

However, when you run a command with sudo, the UID of the sudo process and its child process is set to 0 (root); in general, unless the UID of the process sending the signal is 0 (root) or the same as the target process, the kernel dismisses the signal (i.e.: a process can't send signals to a process owned by another user, unless the process sending the signal is owned by root); that's why an user-run process such as the Bash instance run by the terminal can't SIGHUP a sudo process and, ultimately, closing a terminal doesn't affect a process started with sudo.

Related Question