Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ubuntu 利用 crontab 和 notify-send 定时发送桌面通知,提示该休息啦 #11

Open
Bakumon opened this issue Feb 7, 2017 · 0 comments
Labels

Comments

@Bakumon
Copy link
Owner

Bakumon commented Feb 7, 2017

版权声明:本文为 Bakumon 原创文章,转载需在明确位置注明出处!!!#11


notify-send

notify-send 是 Linux 下发送通知到桌面的命令行程序。Ubuntu16.04 会在桌面右上角出现一个通知提醒框。

基本用法:

  1. 发送一条通知内容
    notify-send "这是通知的内容"

  2. 发送一条有标题的通知
    notify-send "标题" "这是通知的内容"

  3. 发送一条有图片的通知
    notify-send /home/tip.png "标题" "这是通知的内容"

crontab

crontab 是 Linux 下的定时任务,服务端一般用于定时重启某些服务、定时清理日志信息等。

  1. 执行命令
sudo crontab -l

查看定时任务列表,如果有的话,将会列出来

  1. 执行命令
sudo crontab -e

进入编辑 crontab 的模式下。

语法是这样的:

minute hour day month week command
参数 说明
minute 分钟,0到59
hour 小时,0到23
day 日期,1到31
month 月份,1到12
week 星期,0到7,这里的0或7代表星期日
command 要执行的命令,命令行命令,或者外部脚本文件

例子:

  1. 每分钟都执行一次 command
    * * * * * command

  2. 每小时的 2 和 3 分钟时执行一次 command
    2,3 * * * * command

  3. 10 点到 18 点之间每个小时的 2 和 3 分钟都执行一次 command
    2,3 10-18 * * * command

  4. 晚上 11 点到早上 7 点之间,每隔 2 小时重启smb
    * 23-7/2 * * * /etc/init.d/smb restart

结合出功能--每两个小时提示用户该休息啦

执行命令 sudo crontab -e 进入定时任务编辑模式,在最后一行加入以下代码:

0 */2 * * * export DISPLAY=:0.0; notify-send /home/tip.png "休息" "滚去休息吧!!!"

保存退出即可,偶数小时的 0 分钟就会有 “去休息” 的提示。

Note:
要在定时任务里调用图形界面程序,需要加上 export DISPLAY=:0.0; 环境变量,告诉定时任务图形界面程序显示的屏幕。

@Bakumon Bakumon changed the title ubuntu Ubuntu 利用 crontab 和 notify-send 定时发送桌面通知,提示该休息啦 Feb 7, 2017
@Bakumon Bakumon added the Ubuntu label Feb 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant