
これらは両方とも、ロックされた画面が空白になった後にのみ機能しますが、何らかの理由で画面が空白にならない場合は失敗することもあります...
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
アクエリアスパワーの答えかなりうまく機能しているようです。彼の解決策に私が追加するかもしれないいくつかの点を以下に示します。
ロック状態のみを照会する
ロック状態を照会するための 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 スクリプト デーモンに含まれている点が異なっていました。非常に役立ちました。私の質問と回答を見て、これがあなたにも役立つかどうかを確認してください。