
ambos solo funcionan después de que la pantalla bloqueada se borra; pero a veces también fallan, cuando por alguna razón la pantalla no se queda en blanco...
gnome-screensaver-command --query
gnome-screensaver-command --time
Probé con qdbus
también:
qdbus org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.GetActiveTime
pero igualmente fracasó.
¡Acabo de descubrir que quien realmente bloquea la pantalla es Unity!
qdbus com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.Lock
Preguntas relacionadas:
https://unix.stackexchange.com/questions/28181/run-script-on-screen-lock-unlock
https://unix.stackexchange.com/questions/80143/how-to-create-a-daemon-what-would-be-listening-to-dbus-and-fire-script-on-messa
Respuesta1
La respuesta del poder de AcuarioParece funcionar bastante bien. Aquí hay algunas adiciones que podría hacer a su solución.
Solo consultando el estado de bloqueo
Si simplemente necesita una sola línea para consultar el estado de bloqueo, esto debería evaluarse como verdadero si está bloqueado y falso si está desbloqueado.
isLocked=$(gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked | grep -ioP "(true)|(false)")
Consultar el estado de bloqueoyrastrear el tiempo desde el último cambio de estado
Ahora bien, si necesita realizar un seguimiento de cuánto tiempo ha estado bloqueada la pantalla, es posible que desee adoptar un enfoque diferente.
#!/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
}
Básicamente, este script busca cambios en el estado de bloqueo de la pantalla. Cuando se producen cambios, la hora y el estado se vuelcan en un archivo. Puede leer este archivo manualmente si lo desea o utilizar las funciones que escribí.
Si desea una marca de tiempo en lugar de la cantidad de segundos, intente:
date -ud @$(getSecondsElapsed) | grep -oP "(\d{2}:){2}\d{2}"
No olvide el -u
interruptor que obliga al programa de fecha a ignorar su zona horaria.
Respuesta2
la pantalla en realidad está bloqueada por Unity y necesitamos usargdbus
gdbus monitor -e -d com.canonical.Unity -o /com/canonical/Unity/Session
esto se mostrará cuando se bloqueó como:
/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 ()
Respuesta3
yo tenia una pregunta similaraquí
y la ayuda que recibí fue similar a lo que Aquarius Power dijo antes, excepto que estaba incluido en un demonio bash scrip, que puede ejecutarse en segundo plano. Lo encontré muy útil. Entonces, eche un vistazo a mi pregunta y respuesta, y vea si esto también le ayuda a usted.