Answers:
在中创建一个文件/lib/systemd/system-sleep/
,例如lightdm
:
sudo touch /lib/systemd/system-sleep/lightdm
使此文件可执行:
sudo chmod +x /lib/systemd/system-sleep/lightdm
每次您“挂起”或“恢复”您的Ubuntu时,都会运行此脚本。
使用所需的文本编辑器(例如:)将其打开sudo nano /lib/systemd/system-sleep/lightdm
,然后将以下行粘贴到其中,然后保存:
#!/bin/sh
set -e
case "$1" in
pre)
#Store current timestamp (while suspending)
/bin/echo "$(date +%s)" > /tmp/_suspend
;;
post)
#Compute old and current timestamp
oldts="$(cat /tmp/_suspend)"
ts="$(date +%s)"
#Prompt for password if suspended > 10 minutes
if [ $((ts-oldts)) -ge 600 ];
then
export XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
/usr/bin/dm-tool lock
fi
/bin/rm /tmp/_suspend
;;
esac
当您将Ubuntu置于“睡眠”模式时,此脚本将保存当前时间戳,然后在恢复系统时它将检查当前时间戳与旧时间戳的比较,如果差异超过“ 600”秒(10分钟),它将显示您“ lightdm”锁定屏幕,否则不执行任何操作。
最后一步:
打开“系统设置”->“亮度和锁定”。从挂起状态唤醒后,请禁用询问密码,因为我们将处理锁定屏幕留给脚本。
重新启动或关闭后,您仍然需要输入密码。
#Remove password prompet
应读为#Prompt for password if suspended > 10 minutes
/lib/systemd/system-sleep/
如果系统短暂暂停,请添加脚本来解锁会话:
cd /lib/systemd/system-sleep/
sudo touch unlock_early_suspend
sudo chmod 755 unlock_early_suspend
sudo -H gedit unlock_early_suspend
具有以下内容:
#!/bin/bash
# Don't ask for password on resume if computer has been suspended for a short time
# Max duration of unlocked suspend (seconds)
SUSPEND_GRACE_TIME=600
file_time() { stat --format="%Y" "$1"; }
unlock_session()
{
# Ubuntu 16.04
sleep 1; loginctl unlock-sessions
}
# Only interested in suspend/resume events here. For hibernate etc tweak this
if [ "$2" != "suspend" ]; then exit 0; fi
# Suspend
if [ "$1" = "pre" ]; then touch /tmp/last_suspend; fi
# Resume
if [ "$1" = "post" ]; then
touch /tmp/last_resume
last_suspend=`file_time /tmp/last_suspend`
last_resume=`file_time /tmp/last_resume`
suspend_time=$[$last_resume - $last_suspend]
if [ "$suspend_time" -le $SUSPEND_GRACE_TIME ]; then
unlock_session
fi
fi