我怎样才能知道上次屏幕锁定的时间?

有没有一个日志可以查找这些信息?我想知道最近几天我的电脑因为闲置而自动锁定屏幕的时间。

2对于解锁的时间,我执行了以下命令:cat screen /var/log/auth.log | grep unlock - 不需要使用sudo。 - Andrew
3个回答

你可以使用以下命令找到解锁屏幕事件:
grep screen /var/log/auth.log*

但是要找到锁屏事件并不那么简单,因为默认情况下没有任何关于这些事件的日志(据我所知)。
无论如何,您可以运行以下命令来记录锁屏事件:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | ( while true; do read X; if echo "$X" | grep "boolean true" &> /dev/null; then  echo "Screen locked on $(date)" > $HOME/lock_screen.log; fi; done )

~/lock_screen.log文件中。

如果你喜欢上述命令,那么将其用于脚本,并使脚本在启动时自动运行。

参考资料:


2在Ubuntu 17.04上似乎无法正常工作。dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6'"的输出是signal time=1497336035.520628 sender=org.freedesktop.DBus -> destination=:1.140 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired string ":1.140" signal time=1497336035.520706 sender=org.freedesktop.DBus -> destination=:1.140 serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost string ":1.140",然后当我锁定或解锁时没有任何输出。 - maxbellec

对我来说,适用于Ubuntu 16.04.4 LTS和Unity的方法是使用以下命令监视DBUS:

dbus-monitor --session "type='signal',interface='com.canonical.Unity.Session'"

...然后监控“锁定”和“解锁”事件。示例输出:

信号时间=1525269138.855107 发送者=:1.51 -> 目标=(null destination) 序列号=86735 路径=/com/canonical/Unity/Session; 接口=com.canonical.Unity.Session; 成员=LockRequested

信号时间=1525269139.409261 发送者=:1.51 -> 目标=(null destination) 序列号=86892 路径=/com/canonical/Unity/Session; 接口=com.canonical.Unity.Session; 成员=Locked

信号时间=1525269151.238899 发送者=:1.51 -> 目标=(null destination) 序列号=86937 路径=/com/canonical/Unity/Session; 接口=com.canonical.Unity.Session; 成员=UnlockRequested

信号时间=1525269151.791874 发送者=:1.51 -> 目标=(null destination) 序列号=86938 路径=/com/canonical/Unity/Session; 接口=com.canonical.Unity.Session; 成员=Unlocked


这是我在Ubuntu 16.04上使用的。它会记录到系统的syslog中。
将其添加到您的主文件夹中,标记为可执行文件,然后使用gnome-session-properties来配置它在会话启动时运行。
#!/bin/bash

exit_report(){
logger "$(date) Lockscreen Monitoring Terminated."
}
trap "exit_report; exit;" 0

lockmon() {
adddate() {
    while IFS= read -r line; do
      echo $line | grep string | grep '"start"' -q
      if [ $? -eq 0 ] ; then
        logger "$(date) Screen locked"
      fi
      echo $line | grep string | grep '"stop"' -q
      if [ $? -eq 0 ] ; then
        logger "$(date) Screen unlocked"
      fi
    done
}
logger "$(date) Lockscreen Monitoring Started."
dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6.Instance'" | adddate
}

lockmon

基于类似的 Fedora 系统答案。