ubuntu下利用cron来提醒自己休息

原来使用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
发布了217 篇原创文章 · 获赞 8 · 访问量 69万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章