
使用 Unity 運行 Ubuntu 16.04,我想要
- 當我的筆記型電腦閒置 5 分鐘後,關閉螢幕
因不活動而關閉螢幕 1 分鐘後,鎖定螢幕。
當我合上筆記型電腦蓋子時,不要立即鎖定螢幕,而是像蓋子仍然打開一樣,在 5+1 分鐘(或類似時間)後鎖定。
在系統設定→亮度和鎖定,如果設定鎖並ON配置適當的時間(1 分鐘),它會在因不活動而關閉後相應地鎖定螢幕。然而,它也會在關閉蓋子時立即鎖定螢幕,這是我不想要的。
將其設定為OFF使其在關閉蓋子時不會鎖定螢幕,但也會在因不活動而關閉螢幕後保持解鎖狀態。
我已經在 dconf 中將“lid-close-actions”設定為“nothing”:
$ gsettings get org.gnome.settings-daemon.plugins.power lid-close-ac-action
'nothing'
$ gsettings get org.gnome.settings-daemon.plugins.power lid-close-battery-action
'nothing'
如何讓 Ubuntu 僅在用戶不活動一段特定時間後才鎖定螢幕,無論蓋子位置如何?
答案1
下面的後台腳本將幾乎完全按照您的描述進行操作
- x 秒後關閉螢幕
- y 秒後鎖定螢幕
劇本
#!/usr/bin/env python3
import subprocess
import time
# set screen off after x seconds
off = 300
# lock screen after x seconds
lock = 360
# check idle every x seconds
res = 3
def get(cmd):
return subprocess.check_output(cmd).decode("utf-8").strip()
def test(t, threshold):
return int(t)/1000 < threshold
testoff1 = True
testlock1 = True
t1 = 0
while True:
time.sleep(res)
t2 = get("xprintidle")
testoff2 = test(t2, off); testlock2 = test(t2, lock)
if (testoff2, testoff1) == (False, True):
subprocess.Popen(["xset", "dpms", "force", "off"])
if (testlock2, testlock1) == (False, True):
subprocess.Popen(["gnome-screensaver-command", "-l"])
testoff1 = testoff2; testlock1 = testlock2
如何使用
正如您所提到的,您需要關閉現有的蓋子操作:
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action 'nothing'
和:
gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action 'nothing'
而且:
gsettings set org.gnome.desktop.screensaver lock-enabled false
和
gsettings set org.gnome.desktop.session idle-delay 0
因為我們現在處理我們自己的程式。
然後:
該腳本使用
xprintidle
, 來檢查空閒時間sudo apt-get install xprintidle
- 將腳本複製到一個空文件中,另存為
set_times.py
在腳本的頭部,設定關閉螢幕的空閒時間(以秒為單位):
# set screen off after x seconds off = 300
以及鎖定螢幕的時間:
# lock screen after x seconds lock = 360
這些時間是彼此獨立設定的,您可以按您喜歡的任何順序設定(先鎖定,然後關閉,或相反)
你能設定時間“解析度”,檢查空閒時間的頻率(因此設定的時間被四捨五入):
# check idle every x seconds res = 3
但你也可以
測試-運行它:
python3 /path/to/set_times.py
如果一切正常,請將其新增至啟動應用程式:Dash > 啟動應用程式 > 新增。新增指令:
python3 /path/to/set_times.py
概念解釋
- 此命令
xprintidle
返回當前空閒時間(沒有滑鼠或鍵盤輸入) - 然後該腳本測試是否設定時間小於 a定義的閾值,並將狀態與幾秒鐘前進行比較。
如果狀態變更(True --> False),則可以採取任何動作。這是為關閉螢幕完成的,運行:
xset dpms force off
和鎖定螢幕,運行:
gnome-screensaver-command -l
筆記
當然,我們也可以設定關閉和鎖定螢幕的時間論點運行腳本:
#!/usr/bin/env python3 import subprocess import time import sys off = int(sys.argv[1]) if len(sys.argv) > 1 else 300 lock = int(sys.argv[2]) if len(sys.argv) > 2 else off + 60 # check idle every x seconds res = int(sys.argv[3]) if len(sys.argv) > 3 else 5 def get(cmd): return subprocess.check_output(cmd).decode("utf-8").strip() def test(t, threshold): return int(t)/1000 < threshold testoff1 = True testlock1 = True t1 = 0 while True: time.sleep(res) t2 = get("xprintidle") testoff2 = test(t2, off); testlock2 = test(t2, lock) if (testoff2, testoff1) == (False, True): subprocess.Popen(["xset", "dpms", "force", "off"]) if (testlock2, testlock1) == (False, True): subprocess.Popen(["gnome-screensaver-command", "-l"]) testoff1 = testoff2; testlock1 = testlock2
然後運行:
python3 /path/to/set_times.py 300 360
五分鐘後關閉螢幕,六分鐘後鎖定螢幕。
腳本的額外負擔為零。