Search code examples
linuxwindowsconsoleuser-controlswsl-2

How can I check if I exit back to the ps terminal?


The question may sound a bit strange.

The situation is as following:

  • I execute the command "wsl" in the powershell console. My default distro starts or get restored.
  • I execute some commands, if I need root rights, I switch to "sudo -i". Then I exit and get back to previous user. If I exit one time more (maybe accidently) I return back to powershell environment. Or I even exit accidently the console completely. This behaviour I don't like, but this isn't the point.

Now I want to somehow record when I leave the last session in Linux. I tried it the other way around by adding a command in wsl.conf:

command=touch /tmp/test.xyz

Unfortunately, that doesn't help, as the command apparently only runs once after "wsl --shutdown" (at least I think so). My idea was to remove the temp file after my script is done with it; to use it like a switch.

Now my new idea is to use a "bash_logout" script. But I have no idea how I could check if the current user is the first (or last if I exit the current). Commands like

w
who
users

seems only show the current user not any "inherits".

And something like

exit & su

Seems not to be working.

So how can I check if my current console session would be the last if I would exit it?

Maybe I'm missing something obvious, if so, I'm sorry. I try to get better with linux.


Solution

  • If you want a programmatic way of checking try examining the output from the following:

    echo -e "User: $USER\nSUDO_USER: $SUDO_USER\nLOGNAME: $LOGNAME\nlogname: $(logname)"

    Start with your own user, then change around via sudo and su etc., depending on the Linux distro, sudo version, and potentially the shell (bash, zsh, etc.) you may get different results then I do. I've also observed SUDO_USER not get populated on some older server distributions.


    On a fairly recent distribution this produced:

    If thedude logs in as the first user: the SUDO_USER environment variable is empty; both USER and LOGNAME environment variables have a value of thedude; logname command output is thedude:

    User: thedude
    SUDO_USER: 
    LOGNAME: thedude
    logname: thedude
    

    If thedude then uses sudo -i to change to root:

    User: root
    SUDO_USER: thedude
    LOGNAME: root
    logname: thedude
    

    Then from root uses su walter to become walter:

    User: walter
    SUDO_USER: thedude
    LOGNAME: walter
    logname: thedude
    

    but if it was instead of su walter it was sudo su walter to become walter, stacking a second sudo command SUDO_USER gets overwritten with root, but logname still has a value of thedude, showing it is the first login:

    User: walter
    SUDO_USER: root
    LOGNAME: walter
    logname: thedude
    

    I use the etckeeper project with .bash_logout to force a commit any time /etc has been altered with the commit message of:

    Session exit commit:
    * User: $USER
    * SUDO_USER: $SUDO_USER
    * LOGNAME: $LOGNAME
    * logname: $(logname)
    

    but for you, this could be a comparison like:

    if [[ "${USER}" = "${LOGNAME}" ]] && [[ "${USER}" = "$(logname)" ]]; then
        # First user and last to logout of host. Do something.
        touch /tmp/test.xyz
    fi