统一 – 如何检测屏幕是否被锁定?

这两个仅在被锁定的屏幕被消隐后才起作用; 但他们有时也会失败,因为任何原因屏幕都没有空白……

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待监听到DBUS和火脚本-ON-MESSA

Aquarius Power的答案看起来效果很好。 以下是我可能对他的解决方案做的一些补充。

仅查询锁定状态

如果您只需要一个单行程序来查询锁定状态,则如果锁定则应评估为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开关强制日期程序忽略您的时区。

屏幕实际上是由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 () 

我在这里有类似的问题

我得到的帮助类似于Aquarius Power之前所说的,除了它包含在一个bash脚本守护程序中,它可以在后台运行..我发现它非常有用。 所以,看看我的问题,然后回答,看看这对你有帮助。