原來使用windows的時候用愛眼衛士,提醒自己一個小時起來活動一下,現在換成了ubuntu,於是就想用cron直接提醒自己休息一下
原理很簡單,每隔一個小時就鎖屏一次
DISPLAY=:0
00 * * * * /usr/bin/gnome-screensaver-command --lock
注意上面的DISPLAY=:0,如果沒這個變量,gnome-screensaver-command不會起效,而會報一條錯誤
Failed to get session bus: Error spawning command line `dbus-launch --autolaunch=903fc0c1b3535fe8f55930a05120822c --binary-syntax --close-stderr': Child process exited with code 1
如果restart了gnome-sessoin那麼你的DISPLAY可能就不是:0了,因此寫了一個腳本爲自動獲取當前的DISPLAY值
#!/bin/sh
if [ "$DISPLAY" != "" ]; then
echo $DISPLAY
exit
fi
if [ "$USER" = "" ]; then
USER=`whoami`
fi
pinky -fw|awk -v user=$USER 'NF == 6{if($1 == user){ print $6}}'|awk 'NR==1{print $0}'
00 * * * * display=`/home/hoping/bin/initx` && export DISPLAY=$display && /usr/bin/gnome-screensaver-command --lock