Results 1 to 2 of 2

Thread: How to detect when a user has closed a graphical session?

  1. #1
    Join Date
    Sep 2011
    Beans
    28

    Question How to detect when a user has closed a graphical session?

    Hi,

    I use Xubuntu. I've setup a small script that starts when I log in and continuously checks things:

    Code:
    #!/bin/bash
    
    # Main loop
    while [ 1 ]
    do
      # ... do stuff
      sleep 5
    done
    
    exit 0
    Unfortunately, when I log off and log in as another user, the script is still running... Every time I have to kill it manually.
    I'd like to instruct this script to stop when the user that started it logs off. I tried to use the command who, but it seems to detect only the users that have opened a terminal.

    Thank you in advance

  2. #2
    Join Date
    Sep 2011
    Beans
    28

    Re: How to detect when a user has closed a graphical session?

    I probably found a solution (for xfce environments only)

    In short, I modified the script to save the current user in a variable before starting the while loop; then it checks the current owner of the xfdesktop process in every loop. If the two do not match anymore, the script breaks the loop and exits:

    Code:
    #!/bin/bash
    
    ORIGINAL_USER="$( id -u -n )"
    
    # Main loop
    while [ 1 ]
    do
      # ... do stuff
    
      # Check current user; if it changed, exit
      CURRENT_USER="$( ps aux | grep [x]fdesktop | awk '{print $1}' )"
      if [ ! "$CURRENT_USER" == "$ORIGINAL_USER" ]; then
        break
      fi
    
      sleep 5
    done
    
    exit 0
    I guess that a more general (i.e. valid for all graphical environments) solution exists, but in my box it works
    Last edited by zedixal; May 10th, 2013 at 04:54 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •