Ubuntu 定时锁屏改进
年轻的时候还好,不用休息打CS通宵也没问题,前几年终于受不了,脖子不舒服肩膀不舒服。所以前几年就写了第一个版本的,定时30分钟自动锁屏,然后修改键盘布局不让自己输入密码,达到强制工作30分钟休息3分钟的目的。
昨天把老Ubuntu升级到20.04,还可以吧,其实和18亦相差无几,Thinkpad X1小红点变流畅了。那就顺便修改一下锁屏的功能。
第一个版本是用 /var/log/auth.log
实现,通过检查解锁屏幕时间后的30分钟为关屏幕时间,这样也有不好的地方,灵活性差,导致有时候锁屏的时机不对。比如说,我工作了15分钟,看了一下手机用了1分钟,屏幕自动锁屏了,重新打开,它又重新计时了,但我并没有休息。
新版本改用xprintidle,通过检测鼠标键盘操作,控制了要3分钟以上不操作键盘和鼠标才算是休息,这样就更好了。
当然细节还有很多问题,比如说把手机也锁上。。。边用边想边修改叭。
#!/bin/sh
# Auth: Zagfai
# lock screen and stop working
echo "Started Restme"
# config env
export DISPLAY=:0
export XDG_SESSION_TYPE=x11
user=$(whoami)
fl=$(find /proc -maxdepth 2 -user $user -name environ -print -quit)
i=0
while [ -z $(grep -z DBUS_SESSION_BUS_ADDRESS "$fl" | cut -d= -f2- | tr -d '\000' ) ]
do
fl=$(find /proc -maxdepth 2 -user $user -name environ -newer "$fl" -print -quit)
if [ "$i" -eq 10 ];
then
exit;
fi
sleep 1
i="$((i+1))"
done
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS "$fl" | cut -d= -f2-)
# check last unlock screen time
# cat /var/log/auth.log | grep unlocked | tail -1 | awk '{print $1, $2, $3}' > /tmp/unlocktime
# cat /tmp/unlocktime
# python3 -c "import sys;import dateutil.parser;import datetime;to_stop=datetime.timedelta(0, 60*36) >= datetime.datetime.now() - dateutil.parser.parse(open('/tmp/unlocktime').read()) > datetime.timedelta(0, 60*35);sys.exit(0 if to_stop else 1)"
# if [ "$?" -ne 0 ]; then exit; fi
# if no idle file create one
if [ ! -f /tmp/user_last_idle_time ]; then
echo "$(date +%s)" > /tmp/user_last_idle_time
fi
# set idle if already idle for more than 150 seconds ( more than 3 mins)
idletime=$(( $(xprintidle) / 1000 ))
if [ "$idletime" -gt 150 ];
then
echo "$(date +%s)" > /tmp/user_last_idle_time
fi
# more than one night after system suspended
if [ $(( $(date +%s) - $(cat /tmp/user_last_idle_time) )) -gt 10800 ]; # 3 hours
then
echo "$(date +%s)" > /tmp/user_last_idle_time
fi
# less then 30 mins, do not dim screen
if [ $(( $(date +%s) - $(cat /tmp/user_last_idle_time) )) -lt 1800 ];
then
exit;
fi
echo $(( $(cat /tmp/user_last_idle_time) + 300 )) > /tmp/user_last_idle_time
show loading
(
for i in {1..100};
do
sleep 0.28
if [ "$i" -eq 1 ];
then
wmctrl -i -r $(wmctrl -l | grep 'Session Locking' | awk '{print $1}') -b add,sticky,above
fi
if [ "$i" -eq 80 ];
then
wmctrl -i -r $(wmctrl -l | grep 'Session Locking' | awk '{print $1}') -b remove,sticky,above
fi
echo "$i"
done
) |
zenity --progress \
--title="Session Locking" \
--text="\n Have a rest now! Please ready in 20 seconds. \n\n" \
--auto-close \
--auto-kill \
--no-cancel \
# xdotool key Super+l
gnome-screensaver-command -l
# notify-send "Dim Screen"
# xmodmap -e "keycode 48 = NoSymbol"
# sleep 180
# xmodmap -e "keycode 48 = apostrophe quotedbl"