Ubuntu – What processes can survive logging out of Linux, and what not

linuxlogoutprocesssignalsUbuntu

I am trying to understand what happens when we log out of Linux (Ubuntu specifically):

  • How does a process initially request/notify the logging out process to perform logging out (by sending some signals, or some other IPC means)? What is the program run by the logging out process? What is usually the program run by the requesting/notifying process?

  • What processes will the logging out process kill and what not? (There are ways to make processes started after logging in survive logging out, and how do they manage to do that? Making a process not having a controlling terminal seems to be a way, but most of the processes killed by logging out in the following examples don't have a controlling terminal)

  • How does the logging out process kill those processes (by sending some signals, or some other IPC means)?

Consider three cases: virtual console and desktop environment and SSH. (The first two are provided by the OS, and the third isn't, though all are running in user space)

  1. When I log in virtual console, I get the following
    ancestry processes from the login shell:

    1 systemd
    721 login
    26284 bash
    

    After I log out, processes 721 login and below disappear.

  2. On Lubuntu 18.04, when I log in lightdm and LXDE, and run lxterminal
    from the desktop's panel and get the ancestor processes from the
    following command in the lxterminal window:

    $ ps -paus $$
    systemd,1 --system --deserialize 19
      `-lightdm,661
          `-lightdm,27302 --session-child 13 24
              `-lxsession,27309,testme -s Lubuntu -e LXDE
                  `-lxpanel,27399 --profile Lubuntu
                      `-lxterminal,27565
                          `-bash,27568
                              `-pstree,27594 -paus 27568
    

    When I log out, the processes lightdm,27302 and below
    disappear.

  3. After I ssh into Lubuntu 18.04:

    $ pstree -a -p   -s $$
    systemd,1 --system --deserialize 19
      └─sshd,669 -D
          └─sshd,22838 
              └─sshd,22979  
                  └─bash,22980
                      └─pstree,30610 -a -p -s 22980
    

    After I log out, all the processes starting from sshd,22838 to
    below disappear.

Thanks.

Best Answer

I don't know how systemd (especially /usr/lib/systemd/systemd-logind) is involved in OS logout process.

Following is my partial answers:

  1. The login process wait() on the login shell process, and when the login shell exits, login will receive SIGCHLD and wake up to resume its execution, which is to also exit.

  2. It seems that a logout request program such as lxsession-logout sends SIGTERM to the lxsession process (I said "seems" because I don't quite understand the source code). I don't know how lxsession responds to SIGTERM and why its descendants also die, as I can't find its disposition. I don't know why the parent lightdm,27302 of lxsession also die, and guess the parent lightdm,27302 may wait() on lxsession and die once wake up.

  3. I don't know what happens when I log out of SSH. I guess it is similar to CLI login (case 1), sshd,22979 might wait on bash,22980 and die. but I really don't know why there are so many sshd processes.

Related Question