关于java:使用终端工具给你的电脑发送弹窗提醒

4次阅读

共计 3102 个字符,预计需要花费 8 分钟才能阅读完成。

大家好,我是良许。

当初人手一部智能手机,这些智能手机都有个十分实用的性能,那就是弹窗揭示。当咱们收到短信,或者微信信息时,手机就会弹窗显示信息的大抵内容。有了这个性能你就不会错过重要信息了。

电脑上也有相似的性能,也很实用。但这个性能都是零碎级别,咱们能不能通过脚本形式去调用这个弹窗性能呢?

答案是必定的!

例如,当脚本或 cron 工作实现时,长时间运行的编译工作失败,或者脚本执行过程中呈现紧急问题,这些状况下如果能在电脑上弹出一条揭示,必定会让隔壁的美女共事另眼相看!

以下代码已在 Linux 零碎上编写并测试通过,也能够移植到 Mac 电脑上。

从 Linux 终端发送弹窗告诉

要从 Linux 终端发送告诉,须要应用 notify-send 命令。这个命令大部分发行版都没有默认装置,须要咱们自行入手。

在 Fedora 上,输出:

$ sudo dnf install notify-send

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

$ sudo apt install notify-send

几个简略弹窗告诉的例子:

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

这个命令不仅反对弹窗,还能够批改紧急水平、自定义图标等。更多信息能够通过 man notify-send 来查问。

你还能够在告诉注释中应用一小段 HTML 标记来为你的信息减少一些格局,比方:加粗、斜体,等等。最重要的是,URL 还反对点击,十分不便。例如:

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

发送的告诉跟零碎的其它告诉款式一样,外观、行为并无二致。

联合 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
  • 非凡工夫,例如 noonmidnight

利用 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"
}

好好玩玩吧!


学习编程,千万不要急于求成,肯定要多读一些经典书籍,多看源码,多下苦功夫去死磕代码,这样技术能力出息。给大家分享一些程序员必读经典书籍,肯定要多读几遍:

收费送给大家,只求大家金指给我点个赞!

程序员必读经典书单(高清 PDF 版)

有播种?心愿老铁们来个三连击,给更多的人看到这篇文章

举荐浏览:

  • 干货 | 程序员进阶架构师必备资源免费送
  • 刷题 | LeetCode 算法刷题神器,看完 BAT 随你挑!

欢送关注我的博客:良许 Linux 教程网,满满都是干货!

正文完
 0