定时提示休息脚本
ubuntu没有好用的定时提醒休息的软件,workrave,safeeyes都试过,计时总是会被重置,无法按时提醒。有网友分享利用notify-send和crontab,gnome-screensaver实现简单的提醒锁屏。
问题描述
在crontab下执行notify-send无法弹出窗口
原因分析:
没有设置XDG_RUNTIME_DIR
解决方案:
网上有很多方法,比如:
- notify-send的执行路径为绝对路径(无效)
which nofity-send
* * * * * /usr/bin/notify-send -u normal -t 500 -i appointment-new '1分钟后锁屏' '注意休息,善待眼睛'
- 配置DISPLAY(无效)
* * * * * export DISPLAY=:0.0;notify-send -u normal -t 500 -i appointment-new '1分钟后锁屏' '注意休息,善待眼睛'
- 最后借鉴了一个网友的分享,需要配置XDG_RUNTIME_DIR,成功解决:
* * * * * XDG_RUNTIME_DIR=/run/user/$(id -u) /usr/bin/notify-send -u normal -t 500 -i appointment-new '1分钟后锁屏' '注意休息,善待眼睛'
完整的crontab配置:
c/crontab buffers
1 #DISPLAY=:0
2 # Edit this file to introduce tasks to be run by cron.
3 #
4 # Each task to run has to be defined through a single line
5 # indicating with different fields when the task will be run
6 # and what command to run for the task
7 #
8 # To define the time you can provide concrete values for
9 # minute (m), hour (h), day of month (dom), month (mon),
10 # and day of week (dow) or use '*' in these fields (for 'any').#
11 # Notice that tasks will be started based on the cron's system
12 # daemon's notion of time and timezones.
13 #
14 # Output of the crontab jobs (including errors) is sent through
15 # email to the user the crontab file belongs to (unless redirected).
16 #
17 # For example, you can run a backup of all your user accounts
18 # at 5 a.m every week with:
19 # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
20 #
21 # For more information see the manual pages of crontab(5) and cron(8)
22 #
23 # m h dom mon dow command
24
25
26 58 * * * * XDG_RUNTIME_DIR=/run/user/$(id -u) /usr/bin/notify-send -u normal -t 500 -i appointment-new '1分钟后锁屏' '注意休息,善待眼睛'
27 59 * * * * export DISPLAY=:0.0;XDG_RUNTIME_DIR=/run/user/$(id -u) gnome-screensaver-command -l
参考:https://blog.youkuaiyun.com/shjinyuan/article/details/106265238