解决crontab定时执行notify-send无法弹窗(ubuntu18.04)

定时提示休息脚本

ubuntu没有好用的定时提醒休息的软件,workrave,safeeyes都试过,计时总是会被重置,无法按时提醒。有网友分享利用notify-send和crontab,gnome-screensaver实现简单的提醒锁屏。

问题描述

在crontab下执行notify-send无法弹出窗口


原因分析:

没有设置XDG_RUNTIME_DIR


解决方案:

网上有很多方法,比如:

  1. notify-send的执行路径为绝对路径(无效)
which nofity-send
* * * * * /usr/bin/notify-send -u normal -t 500 -i appointment-new '1分钟后锁屏' '注意休息,善待眼睛'
  1. 配置DISPLAY(无效)
* * * * * export DISPLAY=:0.0;notify-send -u normal -t 500 -i appointment-new '1分钟后锁屏' '注意休息,善待眼睛'
  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.csdn.net/shjinyuan/article/details/106265238

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@daviiid

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值