使用終端工具給你的電腦發(fā)送彈窗提醒!
大家好,我是良許。
現(xiàn)在人手一部智能手機,這些智能手機都有個非常實用的功能,那就是彈窗提醒。當(dāng)我們收到短信,或者微信信息時,手機就會彈窗顯示信息的大致內(nèi)容。有了這個功能你就不會錯過重要信息了。
電腦上也有類似的功能,也很實用。但這個功能都是系統(tǒng)級別,我們能不能通過腳本方式去調(diào)用這個彈窗功能呢?
答案是肯定的!
例如,當(dāng)腳本或 cron 任務(wù)完成時,長時間運行的編譯任務(wù)失敗,或者腳本執(zhí)行過程中出現(xiàn)緊急問題,這些情況下如果能在電腦上彈出一條提醒,肯定會讓隔壁的美女同事刮目相看!
以下代碼已在 Linux 系統(tǒng)上編寫并測試通過,也可以移植到 Mac 電腦上。
從 Linux 終端發(fā)送彈窗通知
要從 Linux 終端發(fā)送通知,需要使用 notify-send 命令。這個命令大部分發(fā)行版都沒有默認安裝,需要我們自行動手。
在 Fedora 上,輸入:
- $ sudo dnf install notify-send
在基于 Debian 的發(fā)行版上,鍵入:
- $ sudo apt install notify-send
幾個簡單彈窗通知的例子:
- $ notify-send "liangxu is great!!"
- $ notify-send "welcome to liangxu's website" "www.lxlinux.net"
這個命令不僅支持彈窗,還可以修改緊急程度、自定義圖標(biāo)等。更多信息可以通過 man notify-send 來查詢。
你還可以在通知正文中使用一小段 HTML 標(biāo)記來為你的信息增加一些格式,比如:加粗、斜體,等等。最重要的是,URL 還支持點擊,非常方便。例如:
- $ notify-send -u critical \
- "Build failed!" \
- "There were <b>123</b> errors. Click here to see the results: http://buildserver/latest"
發(fā)送的通知跟系統(tǒng)的其它通知樣式一樣,外觀、行為并無二致。
結(jié)合 at 命令使用 notify-send
cron 命令通常用于定期調(diào)度任務(wù),at 命令則是在指定時間單次執(zhí)行指定命令。如果你像下面這樣運行 at 命令,它會以交互模式啟動,然后你可以在其中輸入你要執(zhí)行的命令:
- $ at 12:00
但我們一般不這么使用它。
at 命令可以接受來自標(biāo)準(zhǔn)輸入的參數(shù),例如:
- $ 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 命令結(jié)合使用,達到在未來的某個時間彈窗提醒的效果。例如:
- $ echo "notify-send 'Stop it and go home now?' 'Enough work for today.' -u critical" | at now
編寫腳本實現(xiàn)彈窗通知功能
現(xiàn)在我們知道 nofity-send 怎么玩了,但每次都要敲這么長的一串命令還是很不方便。
作為程序員,我們能偷懶就偷懶,自己動手寫腳本把這個功能封裝起來!
比如我們把它封裝成一個 Bash 命令 remind ,然后通過下面方式來調(diào)用它:
- $ 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
簡直太特么方便了!
實現(xiàn)起來也很簡單,我們可以將腳本保存在某個位置,例如,在 ~/bin/ 目錄中,并在 .bashrc 配置文件中讓它生效,以便在登錄時加載它:
- $ source ~/bin/remind
腳本內(nèi)容如下:
- #!/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"
- }
好好玩玩吧!