如何在gnome中记录登录/注销和屏幕锁定/解锁

时间:2012-12-18 22:00:29

标签: bash login gnome

我想创建一个日志文件,其中包含某些事件的日志,如:

  • 登录gnome
  • 日志屏幕
  • 解锁屏幕
  • 注销

我的计划是编写一个在后台运行的脚本作为gnome会话的子进程。首先是附加“LOGIN”,监视屏幕锁定/解锁,并在收到SIGHUP时附加“LOGOUT”(意味着会话结束)。

我编写了一个脚本[1],如果我在shell中启动它可以工作,但它很笨重。我希望这个程序在后台运行 - 我不想记得每次登录时都启动它。

有人能指出我正确的方向吗?

[1]剧本:

#!/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
  )

2 个答案:

答案 0 :(得分:2)

我也有这样一个脚本,它可以在autostart目录中使用桌面文件启动它:

$ 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=

答案 1 :(得分:0)

现在我认为听一下LockedHint而不是屏幕保护程序的消息会更好。这样你就不会受到屏幕保护程序实现的束缚。

这是一个简单的脚本:

gdbus monitor -y -d org.freedesktop.login1 | grep LockedHint

给出这个:

/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'LockedHint': <true>}, @as [])
/org/freedesktop/login1/session/_32: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'LockedHint': <false>}, @as [])