这两种方法只有在锁定的屏幕变黑后才有效;但有时它们也会失败,当由于某种原因屏幕没有变黑时……
gnome-screensaver-command --query
gnome-screensaver-command --time
我也尝试过qdbus
:
qdbus org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.GetActiveTime
但它同样失败了。
我刚刚发现,真正锁屏的是Unity!
qdbus com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.Lock
相关问题:
https://unix.stackexchange.com/questions/28181/run-script-on-screen-lock-unlock
https://unix.stackexchange.com/questions/80143/how-to-create-a-daemon-which-would-be-listening-to-dbus-and-fire-script-on-messa
答案1
水瓶座力量的答案看起来效果很好。以下是我可能对他的解决方案做出的一些补充。
仅查询锁状态
如果您只需要一行代码来查询锁定状态,则如果锁定则应计算为 true,如果解锁则计算为 false。
isLocked=$(gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked | grep -ioP "(true)|(false)")
查询锁状态和跟踪自上次状态改变以来的时间
现在,如果您需要跟踪屏幕锁定的时间,您可能需要采取不同的方法。
#!/bin/bash
# To implement this, you can put this at the top of a bash script or you can run
# it the subshell in a separate process and pull the functions into other scripts.
# We need a file to keep track of variable inside subshell the file will contain
# two elements, the state and timestamp of time changed, separated by a tab.
# A timestamp of 0 indicates that the state has not changed since we started
# polling for changes and therefore, the time lapsed in the current state is
# unknown.
vars="/tmp/lock-state"
# start watching the screen lock state
(
# set the initial value for lock state
[ "$(gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked | grep -ioP "(true)|(false)")" == "true" ] && state="locked" || state="unlocked"
printf "%s\t%d" $state 0 > "$vars"
# start watching changes in state
gdbus monitor -e -d com.canonical.Unity -o /com/canonical/Unity/Session | while read line
do
state=$(grep -ioP "((un)?locked)" <<< "$line")
# If the line read denotes a change in state, save it to a file with timestamp for access outside this subshell
[ "$state" != "" ] && printf "%s\t%d" ${state,,} $(date +%s)> "$vars"
done
) & # don't wait for this subshell to finish
# Get the current state from the vars exported in the subshell
function getState {
echo $(cut -f1 "$vars")
}
# Get the time in seconds that has passed since the state last changed
function getSecondsElapsed {
if [ $(cut -f2 "$vars") -ne 0 ]; then
echo $(($(date +%s)-$(cut -f2 "$vars")))
else
echo "unknown"
fi
}
本质上,此脚本会监视屏幕锁定状态的变化。发生更改时,时间和状态会转储到文件中。如果您愿意,可以手动读取此文件,或者使用我编写的函数。
如果您想要时间戳而不是秒数,请尝试:
date -ud @$(getSecondsElapsed) | grep -oP "(\d{2}:){2}\d{2}"
不要忘记-u
强制日期程序忽略您的时区的开关。
答案2
屏幕实际上已被 Unity 锁定,我们需要使用gdbus
gdbus monitor -e -d com.canonical.Unity -o /com/canonical/Unity/Session
这将显示它何时被锁定,如下所示:
/com/canonical/Unity/Session: com.canonical.Unity.Session.LockRequested ()
/com/canonical/Unity/Session: com.canonical.Unity.Session.Locked ()
/com/canonical/Unity/Session: com.canonical.Unity.Session.UnlockRequested ()
/com/canonical/Unity/Session: com.canonical.Unity.Session.Unlocked ()
答案3
我也有类似的问题这里
我得到的帮助与 Aquarius Power 之前所说的类似,只是它包含在一个可以在后台运行的 bash scrip 守护程序中。我发现它非常有用。所以,看看我的问题和答案,看看这是否也对你有帮助。