Sunday, May 5, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 15696  / 1 Year ago, sat, may 27, 2023, 7:58:09

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

More From » command-line

 Answers
0

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.


[#25424] Sunday, May 28, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fulerio

Total Points: 172
Total Questions: 124
Total Answers: 109

Location: Hungary
Member since Thu, Aug 6, 2020
4 Years ago
fulerio questions
;