使用终端工具给你的电脑发送弹窗提醒!

开发 开发工具
现在人手一部智能手机,这些智能手机都有个非常实用的功能,那就是弹窗提醒。当我们收到短信,或者微信信息时,手机就会弹窗显示信息的大致内容。有了这个功能你就不会错过重要信息了。

大家好,我是良许。

现在人手一部智能手机,这些智能手机都有个非常实用的功能,那就是弹窗提醒。当我们收到短信,或者微信信息时,手机就会弹窗显示信息的大致内容。有了这个功能你就不会错过重要信息了。

电脑上也有类似的功能,也很实用。但这个功能都是系统级别,我们能不能通过脚本方式去调用这个弹窗功能呢?

答案是肯定的!

例如,当脚本或 cron 任务完成时,长时间运行的编译任务失败,或者脚本执行过程中出现紧急问题,这些情况下如果能在电脑上弹出一条提醒,肯定会让隔壁的美女同事刮目相看!

以下代码已在 Linux 系统上编写并测试通过,也可以移植到 Mac 电脑上。

从 Linux 终端发送弹窗通知

要从 Linux 终端发送通知,需要使用 notify-send 命令。这个命令大部分发行版都没有默认安装,需要我们自行动手。

在 Fedora 上,输入:

  1. $ sudo dnf install notify-send 

在基于 Debian 的发行版上,键入:

  1. $ sudo apt install notify-send 

几个简单弹窗通知的例子:

  1. $ notify-send "liangxu is great!!" 
  2. $ notify-send "welcome to liangxu's website" "www.lxlinux.net" 

这个命令不仅支持弹窗,还可以修改紧急程度、自定义图标等。更多信息可以通过 man notify-send 来查询。

你还可以在通知正文中使用一小段 HTML 标记来为你的信息增加一些格式,比如:加粗、斜体,等等。最重要的是,URL 还支持点击,非常方便。例如:

  1. $ notify-send -u critical \ 
  2.   "Build failed!" \ 
  3.   "There were <b>123</b> errors. Click here to see the results: http://buildserver/latest" 

发送的通知跟系统的其它通知样式一样,外观、行为并无二致。

结合 at 命令使用 notify-send

cron 命令通常用于定期调度任务,at 命令则是在指定时间单次执行指定命令。如果你像下面这样运行 at 命令,它会以交互模式启动,然后你可以在其中输入你要执行的命令:

  1. at 12:00 

但我们一般不这么使用它。

at 命令可以接受来自标准输入的参数,例如:

  1. $ echo "npm run build" | at now + 1 minute 
  2. $ echo "backup-db" | at 13:00 

熟练使用 Linux 的小伙伴都知道,我们有多种指定时间的方法。

  • 绝对时间,例如 10:00
  • 相对时间,例如 now + 2 hours
  • 特殊时间,例如 noon 或 midnight

利用 at 命令的这些特性,我们可以将它与 notify-send 命令结合使用,达到在未来的某个时间弹窗提醒的效果。例如:

  1. $ echo "notify-send 'Stop it and go home now?' 'Enough work for today.' -u critical" | at now 

编写脚本实现弹窗通知功能

现在我们知道 nofity-send 怎么玩了,但每次都要敲这么长的一串命令还是很不方便。

作为程序员,我们能偷懒就偷懒,自己动手写脚本把这个功能封装起来!

比如我们把它封装成一个 Bash 命令 remind ,然后通过下面方式来调用它:

  1. $ remind "I'm still here" now 
  2. $ remind "Time to wake up!" in 5 minutes 
  3. $ remind "Dinner" in 1 hour 
  4. $ remind "Take a break" at noon 
  5. $ remind "It's Friday pints time!" at 17:00 

简直太特么方便了!

实现起来也很简单,我们可以将脚本保存在某个位置,例如,在 ~/bin/ 目录中,并在 .bashrc 配置文件中让它生效,以便在登录时加载它:

  1. $ source ~/bin/remind 

脚本内容如下:

  1. #!/usr/bin/env bash 
  2. function remind () { 
  3.   local COUNT="$#" 
  4.   local COMMAND="$1" 
  5.   local MESSAGE="$1" 
  6.   local OP="$2" 
  7.   shift 2 
  8.   local WHEN="$@" 
  9.   # Display help if no parameters or help command 
  10.   if [[ $COUNT -eq 0 || "$COMMAND" == "help" || "$COMMAND" == "--help" || "$COMMAND" == "-h" ]]; then 
  11.     echo "COMMAND" 
  12.     echo "    remind <message> <time>" 
  13.     echo "    remind <command>" 
  14.     echo 
  15.     echo "DESCRIPTION" 
  16.     echo "    Displays notification at specified time" 
  17.     echo 
  18.     echo "EXAMPLES" 
  19.     echo '    remind "Hi there" now' 
  20.     echo '    remind "Time to wake up" in 5 minutes' 
  21.     echo '    remind "Dinner" in 1 hour' 
  22.     echo '    remind "Take a break" at noon' 
  23.     echo '    remind "Are you ready?" at 13:00' 
  24.     echo '    remind list' 
  25.     echo '    remind clear' 
  26.     echo '    remind help' 
  27.     echo 
  28.     return 
  29.   fi 
  30.   # Check presence of AT command 
  31.   if ! which at >/dev/nullthen 
  32.     echo "remind: AT utility is required but not installed on your system. Install it with your package manager of choice, for example 'sudo apt install at'." 
  33.     return 
  34.   fi 
  35.   # Run commands: list, clear 
  36.   if [[ $COUNT -eq 1 ]]; then 
  37.     if [[ "$COMMAND" == "list" ]]; then 
  38.       at -l 
  39.     elif [[ "$COMMAND" == "clear" ]]; then 
  40.       at -r $(atq | cut -f1) 
  41.     else 
  42.       echo "remind: unknown command $COMMAND. Type 'remind' without any parameters to see syntax." 
  43.     fi 
  44.     return 
  45.   fi 
  46.   # Determine time of notification 
  47.   if [[ "$OP" == "in" ]]; then 
  48.     local TIME="now + $WHEN" 
  49.   elif [[ "$OP" == "at" ]]; then 
  50.     local TIME="$WHEN" 
  51.   elif [[ "$OP" == "now" ]]; then 
  52.     local TIME="now" 
  53.   else 
  54.     echo "remind: invalid time operator $OP" 
  55.     return 
  56.   fi 
  57.   # Schedule the notification 
  58.   echo "notify-send '$MESSAGE' 'Reminder' -u critical" | at $TIME 2>/dev/null 
  59.   echo "Notification scheduled at $TIME" 

 

好好玩玩吧!

 

责任编辑:武晓燕 来源: 良许Linux
相关推荐

2022-07-14 15:00:53

Linux 终端通知脚本命令

2016-11-09 10:01:04

软考备考IT行业

2021-08-11 14:34:10

Linux文件管理器

2023-01-30 16:21:24

Linux外观

2021-03-13 21:00:30

电脑PC电脑弹窗广告

2021-03-01 13:11:20

duf终端工具Linux

2021-03-02 08:49:53

tmuxLinux命令

2024-02-06 10:06:32

Windows 11Windows 10微软

2024-01-05 12:03:37

终端工具​tmux

2021-08-05 13:20:46

Python工具工具

2021-04-01 10:22:42

工具Linux文件

2011-08-21 09:07:10

2021-08-01 22:59:16

Python工具开发

2022-02-08 15:37:22

微软Windows 11

2011-02-14 16:47:48

华为平板电脑

2014-06-18 10:47:05

dstat监控工具

2021-01-27 13:16:39

ScreenLinux命令

2021-02-16 10:58:50

ScreenLinux命令

2021-08-28 23:23:01

电脑广告程序

2021-05-18 12:02:03

GoTTY终端工具Web
点赞
收藏

51CTO技术栈公众号