5

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

  • 登录 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
  )
4

2 回答 2

2

我也有这样的脚本,它可以很好地使用自动启动目录中的桌面文件启动它:

$ 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=
于 2012-12-18T23:43:02.570 回答
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 [])
于 2018-06-10T23:35:13.500 回答