您现在的位置是:亿华云 > 数据库
使用终端工具给你的电脑发送弹窗提醒!
亿华云2025-10-03 15:00:27【数据库】8人已围观
简介大家好,我是良许。现在人手一部智能手机,这些智能手机都有个非常实用的功能,那就是弹窗提醒。当我们收到短信,或者微信信息时,手机就会弹窗显示信息的大致内容。有了这个功能你就不会错过重要信息了。电脑上也有
大家好,使用我是终端良许。
现在人手一部智能手机,工具这些智能手机都有个非常实用的电脑弹窗功能,那就是发送弹窗提醒。当我们收到短信,提醒或者微信信息时,使用手机就会弹窗显示信息的终端大致内容。有了这个功能你就不会错过重要信息了。工具
电脑上也有类似的电脑弹窗功能,也很实用。发送但这个功能都是提醒系统级别,我们能不能通过脚本方式去调用这个弹窗功能呢?使用
答案是肯定的!
例如,当脚本或 cron 任务完成时,终端长时间运行的工具编译任务失败,或者脚本执行过程中出现紧急问题,这些情况下如果能在电脑上弹出一条提醒,肯定会让隔壁的美女同事刮目相看!
以下代码已在 Linux 系统上编写并测试通过,也可以移植到 Mac 电脑上。
从 Linux 终端发送弹窗通知
要从 Linux 终端发送通知,需要使用 notify-send 命令。云南idc服务商这个命令大部分发行版都没有默认安装,需要我们自行动手。
在 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"发送的通知跟系统的其它通知样式一样,外观、行为并无二致。
结合 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 "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 () { 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" }好好玩玩吧!
源码下载很赞哦!(11878)