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

Please post suggestions for improvement of Cinnamon on:
https://github.com/linuxmint/Cinnamon
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
gurtz
Level 1
Level 1
Posts: 49
Joined: Wed Jan 19, 2011 10:06 pm

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

Post by gurtz »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
gtsfer

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

Post by gtsfer »

Never tried it myself, but these commands may be of some help. http://askubuntu.com/questions/15795/ho ... e-terminal
gurtz
Level 1
Level 1
Posts: 49
Joined: Wed Jan 19, 2011 10:06 pm

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

Post by gurtz »

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.
gtsfer

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

Post by gtsfer »

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/
gurtz
Level 1
Level 1
Posts: 49
Joined: Wed Jan 19, 2011 10:06 pm

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

Post by gurtz »

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
gtsfer

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

Post by gtsfer »

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)
gurtz
Level 1
Level 1
Posts: 49
Joined: Wed Jan 19, 2011 10:06 pm

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

Post by gurtz »

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: 49
Joined: Wed Jan 19, 2011 10:06 pm

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

Post by gurtz »

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
gtsfer

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

Post by gtsfer »

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.
anandrkris

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

Post by anandrkris »

Thanks for sharing, can be handy at times. :)
gurtz
Level 1
Level 1
Posts: 49
Joined: Wed Jan 19, 2011 10:06 pm

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

Post by gurtz »

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).
Locked

Return to “Cinnamon”