3

Can someone tell me how to remove dormant X sessions. This question is similar to Logging out other users from the command line, but more specific to controlling X displays which I find hard to kill.

I used the command "who -u" to get the session of the other screens:

$ who -u

Which gave me:

user1   :0           2014-08-18 12:08   ?          2891 (:0)
user1   pts/26       2014-08-18 16:11 17:18        3984 (:0)
user2   :1           2014-08-18 18:21   ?         25745 (:1)
user1   pts/27       2014-08-18 23:10 00:27        3984 (:0)
user1   pts/32       2014-08-18 23:10 10:42        3984 (:0)
user1   pts/46       2014-08-18 23:14 00:04        3984 (:0)
user1   pts/48       2014-08-19 04:10   .          3984 (:0)

The kill -9 25745 doesn't appear to do anything.

I have a workshop where a number of users will use the computer under their own login. After the workshop is over there are a number of logins that are left open. I would prefer to kill the open sessions rather than try to log into each users' screen.

Again, this question isn't just about logging users' out. I'm hoping to get clarity also for killing/removing stuck processes that are hard to kill.

3
  • This is kind of confusing, because you have your accepted answer in the question itself. You do know that you can answer your own question, correct? This might make it clearer for people who come to this question looking for the answer, and find no answer perse.
    – trysis
    Jun 10, 2015 at 11:00
  • @trysis You're right. I'll do what I can to best serve the community. It would be best to have an accepted answer so that it will be flagged and resolved by people doing a search. I'll fix this. Jun 10, 2015 at 14:11
  • Awesome. Sorry if that came off a little preachy or cranky, I wrote it when I was tired & in a hurry (not a great combination).
    – trysis
    Jun 10, 2015 at 14:23

2 Answers 2

0

I've noticed that the kill command doesn't always work the first time. Also there are times when it'll work, but just have a delayed response.

In the case of killing a login session, it most likely takes time for all the processes to die or be killed. So I wrote a script that checks and follows up with subsequent kills that has always succeeded to log out the desired X session.

#!/bin/bash

results=1   
while [[ $results > 0 ]]
do
    sudo kill -9 25745
    results=$?
    echo -ne "Response:$results..."
    sleep 20
done
1
  • sudo kill -9 25745. What do you want to achieve?
    – A.B.
    Jun 10, 2015 at 14:28
0

To kill all logged in users for :1 use this command:

awk '/\s:1\s/ {system("sudo kill -9 "$6)}' <<< $(who -u)

In your case user2

user2   :1           2014-08-18 18:21   ?         25745 (:1)

Or all sessions for user2 by the username

awk '/^user2\s/ {system("sudo kill -9 "$6)}' <<< $(who -u)
5
  • At a glance it appears (also by your description) that this would kill my session and all the other sessions that are active. If you read the details of the question the effort is to figure get a way to kill "dormant" X logged in users. I wouldn't want to kill the sessions of users who are actually sitting at the console working. Just specific users. Jun 10, 2015 at 15:41
  • The command kills only one session: user2 :1 2014-08-18 18:21 ? 25745 (:1)
    – A.B.
    Jun 10, 2015 at 15:51
  • I see now! It works great. Jun 10, 2015 at 19:16
  • =) Give me an upvote.
    – A.B.
    Jun 10, 2015 at 19:36
  • I'll have to test it when I get a user stuck where the -9 doesn't work (as per the question) The resolution that worked was to make a loop that would keep issuing the kill command on the process until the user is off. The script that I posted below in the accepted answer has iterated as many as 10 times before the user was finally logged off. Are you saying your provide line will continue issuing the kill command multiple times if it doesn't die the first time? By the way, the script in the accepted answer will work for other processes that refuses to die. Jun 10, 2015 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.