小知识:Linux使用终端工具给你的电脑发送弹窗提醒!

%小知识:Linux使用终端工具给你的电脑发送弹窗提醒!-猿站网-插图

大家好,我是良许。

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

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

答案是肯定的!

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

%小知识:Linux使用终端工具给你的电脑发送弹窗提醒!-1猿站网-插图

以下代码已在 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 liangxus 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”

%小知识:Linux使用终端工具给你的电脑发送弹窗提醒!-2猿站网-插图

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

结合 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 

%小知识:Linux使用终端工具给你的电脑发送弹窗提醒!-3猿站网-插图

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

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

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

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

$ remind “Im still here” now  $ remind “Time to wake up!”in 5 minutes  $ remind “Dinner”in 1 hour $ remind “Take a break”at noon  $ remind “Its Friday pints time!”at 17:00 

简直太特么方便了!

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

$ source ~/bin/remind 

脚本内容如下:

#!/usr/bin/env bash  function remind () {    localCOUNT=“$#”   local COMMAND=“$1”   local MESSAGE=“$1”   local OP=“$2”   shift 2    localWHEN=“$@”   # 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 ofAT command    if ! which at >/dev/nullthen     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 timeof notification    if [[ “$OP” == “in” ]]; then     localTIME=“now + $WHEN”   elif [[ “$OP” == “at” ]]; then     localTIME=“$WHEN”   elif [[ “$OP” == “now” ]]; then     localTIME=“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”

好好玩玩吧!

原文链接:https://mp.weixin.qq.com/s/ln-YHil-eDkuXh-Sf4VtKQ

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:对nginx-naxsi白名单规则详解

2023-4-4 3:04:51

建站知识

小知识:Nginx通过geo模块设置白名单的例子

2023-4-4 3:14:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索