gnome-session-quit to logout does not work when run as root with sudo

14,722

Solution 1

I'm not entirely sure what you're trying to do but the reason the sudo command is failing is because you have started the X server as pandya and are not exporting pandya's environment variables that allow you to communicate with the running Gnome session.

So, this will allow you to kill your Gnome session:

sudo -E -u pandya gnome-session-quit

From man sudo:

-E, --preserve-env

Indicates to the security policy that the user wishes to pre‐ serve their existing environment variables. The security policy may return an error if the user does not have permis‐ sion to preserve the environment.


If you want to have a script that runs some commands as root and then want it to be able to log you out, you can do it as follows:

  1. Create the script, without sudo for the privileged commands but with sudo for the logout. For example:

    #!/bin/bash
    
    apt-get install firefox
    sudo -E -u pandya gnome-session-quit
    
  2. Run the script with sudo -E:

    sudo -E /path/to/script
    

The sudo -E script.sh ensures that your env variables are available to the script and the sudo -E -u pandya ensures that they are passed on to the gnome-session-quit call.

Solution 2

I've created this script that does set the DBUS session variables from the gnome-session environment in case you want to logout other users/sessions:

How to restart Gnome-Shell from command line?

function logout() {
    local USERNAME
    export USERNAMES=( ) 
    while [ -n "$1" ]; do case "$1" in
        -* ) break ;;
        *) USERNAMES+=( "$1" ); shift ;;
    esac; done

    for USERNAME in "${USERNAMES[@]}"; do
        local SESSION_PID=$(pgrep -fu "$USERNAME" gnome-session|head -1)
        if [ -n "$SESSION_PID" ]; then
            (
                sudo -u "$USERNAME" cat "/proc/$SESSION_PID/environ" | xargs -0 -n 1 echo export
                echo "gnome-session-quit --logout $@"
            ) | sudo -u "$USERNAME" sh -;
        fi
    done
}
Share:
14,722

Related videos on Youtube

Pandya
Author by

Pandya

Started using Linux and StackExchange since Ubuntu 12.04 LTS. Then Upgraded to 14.04 LTS. Now I am using Debian GNU/Linux on my Laptop and PureOS on old Desktop computer. I recommend visiting the Philosophy of GNU Project As I've replaced Ubuntu with Debian GNU/Linux, Now my question(s) are became off-topic on AskUbuntu. So, I continue to Unix & Linux. The second reason for my shifting to U & L is I found U & L more interesting than AU since AU is only Ubuntu specific whereas U & L is a broad concept and in my opinion U & L deserves generic questions. (I know why SE has AU & U & L both).

Updated on September 18, 2022

Comments

  • Pandya
    Pandya over 1 year

    I am running Ubuntu 14.04 and user name is pandya.

    pandya@pandya-desktop:~$ gnome-session-quit
    

    When above command run without being sudo (sudo su) in terminal, it successfully logs out. enter image description here


    But when running after sudo (as a root),

    root@pandya-desktop:/home/pandya# gnome-session-quit
    

    It gives error :

    ** (gnome-session-quit:3168): WARNING **: Failed to call logout: The name org.gnome.SessionManager was not provided by any .service files
    

    So, because of being root, I tries to running command with sudo -u pandya gnome-session-quit to run command as a user pandya. But it gives same error.

    Then I tries follwing to run as pandya:

    root@pandya-desktop:/home/pandya# su pandya
    pandya@pandya-desktop:~$ gnome-session-quit
    
    ** (gnome-session-quit:3269): WARNING **: Failed to call logout: The name org.gnome.SessionManager was not provided by any .service files
    

    But it gives same error.

    Hence, My question is: How to logout as a root? Because I want to put gnome-session-quit in sctipt which is to be run as root. (I don't want to kill forcefully process but want to normal logout prompt)


    Further Specification:-

    I have script which is to be run as root.

    #! /bin/bash
    ....command to be executed.....
    sudo -u pandya gnome-session-quit
    
    • Louis Matthijssen
      Louis Matthijssen about 10 years
      Have you tried sudo -i -u pandya gnome-session-quit? The -i will emulate a terminal exactly like the user will get.
    • Pandya
      Pandya about 10 years
      @LouisMatthijssen : It also gives same error!
    • terdon
      terdon about 10 years
      Is your script going to be run by root or will you use it with sudo? There is a difference.
    • terdon
      terdon about 10 years
      @LouisMatthijssen that won't work because the -i launches a new login shell, it does not give "a terminal exactly like the user would get" (that would be an interactive, non-login shell) but starts a new login shell instance.
  • terdon
    terdon about 10 years
    @Pandya please clarify your situation. You have mentioned sudo su and sudo. Please edit your question and clarify exactly which commands you are using and exactly what your script is supposed to do. These details are very important. My suggestion will work if you run it as pandya, not if you are already inside a root shell. I assumed you would run your script as sudo script.sh.
  • Pandya
    Pandya about 10 years
    I run script with sudo -E so it logs out successfully.