2
我如何才能登錄我的登錄/註銷和屏幕鎖定/解鎖我想創建一個日誌某些事件等的日誌文件:在GNOME
- 登錄到GNOME
- 日誌屏幕
- 解鎖屏幕
- 註銷
我的計劃是寫在後臺作爲GNOME會話的子進程運行的腳本。它將從追加「登錄」開始,監視屏幕鎖定/解鎖,並在收到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 "[email protected]" >&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 '[email protected]$(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
)