Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log my logins/logouts and screen locks/unlocks in gnome

I want to create a logfile with a log of certain events like:

  • log into gnome
  • log screen
  • unlock screen
  • logout

My plan was to write a script that runs in the background as a child process of the gnome session. It would start by appending "LOGIN", monitor for screen locking/unlocking, and append "LOGOUT" when it received a SIGHUP (meaning the session ended).

I wrote a script [1] which works if I start it in a shell, but it's clunky. I want this program running in the background -- I don't want to have to remember to start it each time I log in.

Can someone point me in the right direction?

[1] The script:

#!/bin/bash
# param $1: type, in:
#     ["SCREEN_LOCKED",
#     "SCREEN_UNLOCKED",
#     "LOGIN",
#     "LOGOUT",
#     "SIGINT",
#     "SIGTERM"]
function write_log {
  if [ -z $1 ]; then
    1="unspecified"
  fi
  echo -e "$1\t$(date)" >> "$LOG"
}

function notify {
  echo "$@" >&2
}

function show_usage {
  notify "Usage: $0 login <address> <logfile>"
  notify "Parameters:"
  notify "  login: You must use the string 'login' to avoid seeing this message."
  notify "  <logfile>: File to store logs."
  notify ""
  notify "This script is designed to go in the bashrc file, and be called in the"
  notify "form of: $0 login '$USER@$(uname -n)' >>/path/to/logfile &"
  notify ""
}

if [ "$#" -eq 0 ]; then
  show_usage
  exit 1
fi
if [ "$1" != "login" ]; then
  show_usage
  notify "Error: first parameter must be the string 'login'."
  exit 1
fi
LOG="$2"
if [ -z "$LOG" ]; then
  notify "Error: please specify a logfile."
  exit 1
elif [ -f "$LOG" ]; then
  # If the logfile exists, verify that the last action was a LOGOUT.
  LASTACTION=$(tail -1 "$LOG" | awk '{print $1}')
  if [ $LASTACTION != "LOGOUT" ]; then
    notify "Logfile '$LOG' exists but last action was not logout: $LASTACTION"
    exit 1
  fi
else
  # If the file does not exist, create it.
  touch "$LOG" || ( notify "Cannot create logfile: '$2'" && exit 1 )
fi

# Begin by logging in:
write_log "LOGIN"

# Handle signals by logging:
trap "write_log 'LOGOUT'; exit" SIGHUP
trap "write_log 'INTERRUPTED_SIGINT'; exit 1" SIGINT
trap "write_log 'INTERRUPTED_SIGTERM'; exit 1" SIGTERM

# Monitor gnome for screen locking. Log these events.
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | \
  (
    while true; do
      read X;
      if echo $X | grep "boolean true" &> /dev/null; then
        write_log "SCREEN_LOCKED"
      elif echo $X | grep "boolean false" &> /dev/null; then
        write_log "SCREEN_UNLOCKED"
      fi
    done
  )
like image 346
Robert Martin Avatar asked Nov 03 '22 08:11

Robert Martin


1 Answers

I also have such a script, it works well starting it with a desktop file in the autostart directory:

$ cat ~/.config/autostart/watcher.sh.desktop 

[Desktop Entry]
Type=Application
Exec=/home/<username>/hg/programs/system/watcher/watcher.sh
Hidden=false
X-GNOME-Autostart-enabled=true
Name[de_DE]=watcher
Name=watcher
Comment[de_DE]=
Comment=
like image 190
BeniBela Avatar answered Nov 15 '22 06:11

BeniBela