[Solved] Log out a user session from command line (Cinnamon)

Please post suggestions for improvement of Cinnamon here
https://github.com/linuxmint/Cinnamon
Post Reply
gurtz
Level 1
Level 1
Posts: 38
Joined: Wed Jan 19, 2011 10:06 pm

[Solved] Log out a user session from command line (Cinnamon)

Post by gurtz » Sat Sep 06, 2014 2:45 pm

Hi,

I have been trying to work out how to force a logout of a user session under both MATE and Cinnamon (I have two separate machines). [I'm going to post this in both forums, since it is relatant to both, and may require different answers. Trust that is OK.]

I need to be able to force a user to log out -- with some warning, or no warning at all (it doesn't really matter) -- but have had no success so far. I understand that "gnome-session-save --kill" is a method that could have been used with previous versions, but it no longer works for Mint 17 (MATE or Cinnamon).

Can anyone give me an idea of how to safely and reliably do this? I'd really appreciate it.

Thanks,
Greg
Last edited by gurtz on Fri Sep 12, 2014 5:00 am, edited 1 time in total.

User avatar
gtsfer
Level 7
Level 7
Posts: 1925
Joined: Mon May 27, 2013 9:15 am
Location: Bucks County PA, USA

Re: Logging out a user session from command line (Cinnamon)

Post by gtsfer » Sat Sep 06, 2014 3:12 pm

Never tried it myself, but these commands may be of some help. http://askubuntu.com/questions/15795/ho ... e-terminal
"There is no gravity, the earth sucks." - Issac Newton @ 75
KDE & Cinnamon 18.1 & Win 10 - ASUS M5A99FX PRO MB, AMD FX-8370 Octo-Core CPU, 16GB RAM, AMD Radeon R7 240

gurtz
Level 1
Level 1
Posts: 38
Joined: Wed Jan 19, 2011 10:06 pm

Re: Logging out a user session from command line (Cinnamon)

Post by gurtz » Sat Sep 06, 2014 3:44 pm

Thanks for the reply. That post was mostly referring to "gnome-session-save" and "gnome-session-quit" (supposedly a successor of the former command). But neither of those exist on Mint 17.

I also tried "sudo pkill -u username". That does kill the session, but also seems to freeze up the PC. [I tried that on MATE. Haven't tried it on Cinnamon yet.]

Thanks again.

User avatar
gtsfer
Level 7
Level 7
Posts: 1925
Joined: Mon May 27, 2013 9:15 am
Location: Bucks County PA, USA

Re: Logging out a user session from command line (Cinnamon)

Post by gtsfer » Sat Sep 06, 2014 4:20 pm

I haven't figured this out yet. Perhaps "kill" or "pkill" will work. Looking in Package Manager for "session" (this is in Cinnamon for instance) there is a cinnamon-session package installed. The "installed files" tab shows there is a file "/usr/bin/cinnamon-session-quit". I'll have to try it and see if it does the trick. This is just a guess mind you, but I'll poke around a bit and see what might work.

http://askubuntu.com/questions/180628/h ... -using-cli
http://www.cyberciti.biz/faq/linux-logout-user-howto/
"There is no gravity, the earth sucks." - Issac Newton @ 75
KDE & Cinnamon 18.1 & Win 10 - ASUS M5A99FX PRO MB, AMD FX-8370 Octo-Core CPU, 16GB RAM, AMD Radeon R7 240

gurtz
Level 1
Level 1
Posts: 38
Joined: Wed Jan 19, 2011 10:06 pm

Re: Logging out a user session from command line (Cinnamon)

Post by gurtz » Sat Sep 06, 2014 4:26 pm

That looks quite promising. I didn't think to just change the name from gnome to cinnamon.

However, checking the man page doesn't show any option for logging out ANOTHER user. I wonder if that would be possible. That said, I'd plan to set up a cron job for the task, and if I run the job as the user I want to log out, that should work, right?

Any further ideas are welcome.

Thanks again for the help!

Greg

User avatar
gtsfer
Level 7
Level 7
Posts: 1925
Joined: Mon May 27, 2013 9:15 am
Location: Bucks County PA, USA

Re: Logging out a user session from command line (Cinnamon)

Post by gtsfer » Sat Sep 06, 2014 4:39 pm

From that first link above, maybe "sudo pkill -u NameOftheUser" would work. I'd have to create a another user to test it but it sounds like it might. Sure, I think that cron job should work too. Once we figure out the right command in the first place. 8)
"There is no gravity, the earth sucks." - Issac Newton @ 75
KDE & Cinnamon 18.1 & Win 10 - ASUS M5A99FX PRO MB, AMD FX-8370 Octo-Core CPU, 16GB RAM, AMD Radeon R7 240

gurtz
Level 1
Level 1
Posts: 38
Joined: Wed Jan 19, 2011 10:06 pm

Re: Logging out a user session from command line (Cinnamon)

Post by gurtz » Sun Sep 07, 2014 6:57 pm

Thanks gtsfer! I really appreciate the help.

It seems that "cinnamon-session-quit --logout --force" is the command that I need, however, I'm running into a problem: How can I get this to run properly from cron?

It runs correctly from a terminal, but from cron it does not. I have verified that the command IS executing in cron (i.e. the scheduling is working correctly), but the target user is not logged out. The only way I have been able to get it to work is by explicitly setting DISPLAY=:0 in the crontab file. This seems to set the appropriate environment variable, which enables cinnamon-session-quit to work. The only problem is that DISPLAY is not going to be a consistent number. Based on the order that users log in, different DISPLAY numbers will be assigned, right? So how can I set this appropriately for the user who's crontab is going to execute the command? Or how can I tell crontab to use all the environment variables for the user in question (including DISPLAY)?

Thanks,
Greg

gurtz
Level 1
Level 1
Posts: 38
Joined: Wed Jan 19, 2011 10:06 pm

Re: Logging out a user session from command line (Cinnamon)

Post by gurtz » Thu Sep 11, 2014 8:11 pm

With a bit of help from a friend and some online digging, I found out how to get the appropriate DISPLAY variable set within cron, thereby enabling cinnamon-session-quit to work correctly.

I created the following script which is then executed from cron as the user to be logged out. It works. The script essentially gets the "display" from the last column of the output of the "who" command. It is formatted wrapped in parentheses. For example, if the display is ":20", the last column shows as "(:20)". Most of the script below is to get that last column from "who", strip off the parentheses, and set the DISPLAY variable.

Code: Select all

#!/bin/bash
TEMP1=`who | grep user_to_log_out | grep tty | awk '{ print $5 }'`
export DISPLAY=`echo "${TEMP1:1:${#string}-1}"`
cinnamon-session-quit --logout --force 
Thanks gtsfer for the help!

Greg

User avatar
gtsfer
Level 7
Level 7
Posts: 1925
Joined: Mon May 27, 2013 9:15 am
Location: Bucks County PA, USA

Re: [Solved] Log out a user session from command line (Cinna

Post by gtsfer » Fri Sep 12, 2014 7:20 am

Hey good work! I got off on some other issues and also life-related stuff. That little script for cron is excellent, glad you got it working.
"There is no gravity, the earth sucks." - Issac Newton @ 75
KDE & Cinnamon 18.1 & Win 10 - ASUS M5A99FX PRO MB, AMD FX-8370 Octo-Core CPU, 16GB RAM, AMD Radeon R7 240

User avatar
anandrkris
Level 5
Level 5
Posts: 947
Joined: Fri Nov 16, 2012 1:13 am
Location: Chennai

Re: [Solved] Log out a user session from command line (Cinna

Post by anandrkris » Mon Sep 29, 2014 12:04 am

Thanks for sharing, can be handy at times. :)
Cheers,
If there is a drive, there will be a path

gurtz
Level 1
Level 1
Posts: 38
Joined: Wed Jan 19, 2011 10:06 pm

Re: [Solved] Log out a user session from command line (Cinna

Post by gurtz » Fri Jul 10, 2015 2:22 pm

Just to add to the solution (for completeness purposes), I also had to add entries similar to the following to the start of the crontab:

USER=user_to_log_out
USERNAME=user_to_log_out
SESSION_MANAGER=local/my_machine_name:@/tmp/.ICE-unix/7758,unix/my_machine_name:/tmp/.ICE-unix/7758
DISPLAY=$DISPLAY

Though it might be possible to just replace "user_to_log_out" and "my_machine_name" with the appropriate values, it would probably be better to run "env" in the environment for these particular variables, and use the actual values (except for DISPLAY, which probably should be set as above).

Post Reply

Return to “Cinnamon”