任经理 徐州网站建设,做电商一年能挣多少,济南网站维护公司,西部中大建设集团有限公司网站大家好#xff0c;我是良许。
现在人手一部智能手机#xff0c;这些智能手机都有个非常实用的功能#xff0c;那就是弹窗提醒。当我们收到短信#xff0c;或者微信信息时#xff0c;手机就会弹窗显示信息的大致内容。有了这个功能你就不会错过重要信息了。
电脑上也有类…大家好我是良许。
现在人手一部智能手机这些智能手机都有个非常实用的功能那就是弹窗提醒。当我们收到短信或者微信信息时手机就会弹窗显示信息的大致内容。有了这个功能你就不会错过重要信息了。
电脑上也有类似的功能也很实用。但这个功能都是系统级别我们能不能通过脚本方式去调用这个弹窗功能呢
答案是肯定的
例如当脚本或 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 liangxus website www.lxlinux.net这个命令不仅支持弹窗还可以修改紧急程度、自定义图标等。更多信息可以通过 man notify-send 来查询。
你还可以在通知正文中使用一小段 HTML 标记来为你的信息增加一些格式比如加粗、斜体等等。最重要的是URL 还支持点击非常方便。例如
$ notify-send -u critical \Build failed! \There were b123/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$1local MESSAGE$1local OP$2shift 2local WHEN$# Display help if no parameters or help commandif [[ $COUNT -eq 0 || $COMMAND help || $COMMAND --help || $COMMAND -h ]]; thenecho COMMANDecho remind message timeecho remind commandechoecho DESCRIPTIONecho Displays notification at specified timeechoecho EXAMPLESecho remind Hi there nowecho remind Time to wake up in 5 minutesecho remind Dinner in 1 hourecho remind Take a break at noonecho remind Are you ready? at 13:00echo remind listecho remind clearecho remind helpechoreturnfi# Check presence of AT commandif ! which at /dev/null; thenecho 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.returnfi# Run commands: list, clearif [[ $COUNT -eq 1 ]]; thenif [[ $COMMAND list ]]; thenat -lelif [[ $COMMAND clear ]]; thenat -r $(atq | cut -f1)elseecho remind: unknown command $COMMAND. Type remind without any parameters to see syntax.fireturnfi# Determine time of notificationif [[ $OP in ]]; thenlocal TIMEnow $WHENelif [[ $OP at ]]; thenlocal TIME$WHENelif [[ $OP now ]]; thenlocal TIMEnowelseecho remind: invalid time operator $OPreturnfi# Schedule the notificationecho notify-send $MESSAGE Reminder -u critical | at $TIME 2/dev/nullecho Notification scheduled at $TIME
}好好玩玩吧 学习编程千万不要急于求成一定要多读一些经典书籍多看源码多下苦功夫去死磕代码这样技术才能长进。给大家分享一些程序员必读经典书籍一定要多读几遍 推荐阅读
干货 | 程序员进阶架构师必备资源免费送刷题 | LeetCode算法刷题神器看完 BAT 随你挑
欢迎关注我的博客良许Linux教程网满满都是干货