如何使用终端工具为你的电脑发送弹窗提醒

目录
文章目录隐藏
  1. 从 Linux 终端发送弹窗通知
  2. 结合 at 命令使用 notify-send
  3. 编写脚本实现弹窗通知功能

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

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

答案是肯定的!

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

如何使用终端工具为你的电脑发送弹窗提醒

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

从 Linux 终端发送弹窗通知

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

在 Fedora 上,输入:

$ sudo dnf install notify-send

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

$ sudo apt install notify-send

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

$ notify-send "mybj is great!!"
$ notify-send "welcome to mybj's website" "www.mybj123.com"

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

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

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

HTML 标记来为你的信息增加一些格式

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

结合 at 命令使用 notify-send

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

$ at 12:00

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

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

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

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

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

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

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

达到在未来的某个时间弹窗提醒的效果

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

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

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

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

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

简直太特么方便了!

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

$ source ~/bin/remind

脚本内容如下:

#!/usr/bin/env bash
function remind () {
  local COUNT="$#"
  local COMMAND="$1"
  local MESSAGE="$1"
  local OP="$2"
  shift 2
  local WHEN="$@"
  # Display help if no parameters or help command
  if [[ $COUNT -eq 0 || "$COMMAND" == "help" || "$COMMAND" == "--help" || "$COMMAND" == "-h" ]]; then
    echo "COMMAND"
    echo "    remind <message> <time>"
    echo "    remind <command>"
    echo
    echo "DESCRIPTION"
    echo "    Displays notification at specified time"
    echo
    echo "EXAMPLES"
    echo '    remind "Hi there" now'
    echo '    remind "Time to wake up" in 5 minutes'
    echo '    remind "Dinner" in 1 hour'
    echo '    remind "Take a break" at noon'
    echo '    remind "Are you ready?" at 13:00'
    echo '    remind list'
    echo '    remind clear'
    echo '    remind help'
    echo
    return
  fi
  # Check presence of AT command
  if ! which at >/dev/null; then
    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'."
    return
  fi
  # Run commands: list, clear
  if [[ $COUNT -eq 1 ]]; then
    if [[ "$COMMAND" == "list" ]]; then
      at -l
    elif [[ "$COMMAND" == "clear" ]]; then
      at -r $(atq | cut -f1)
    else
      echo "remind: unknown command $COMMAND. Type 'remind' without any parameters to see syntax."
    fi
    return
  fi
  # Determine time of notification
  if [[ "$OP" == "in" ]]; then
    local TIME="now + $WHEN"
  elif [[ "$OP" == "at" ]]; then
    local TIME="$WHEN"
  elif [[ "$OP" == "now" ]]; then
    local TIME="now"
  else
    echo "remind: invalid time operator $OP"
    return
  fi
  # Schedule the notification
  echo "notify-send '$MESSAGE' 'Reminder' -u critical" | at $TIME 2>/dev/null
  echo "Notification scheduled at $TIME"
}

下去好好玩吧。

「点点赞赏,手留余香」

0

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » 如何使用终端工具为你的电脑发送弹窗提醒

发表回复