如何從 Linux 終端發(fā)送桌面通知與提醒
這篇教程演示如何使用腳本命令來發(fā)送自己的桌面通知與提醒。
有時候,來自腳本的視覺回饋是很有用的。例如,當一個腳本或計劃任務(wù)完成時,一個長期運行的構(gòu)建任務(wù)失敗時,或者當腳本執(zhí)行中出現(xiàn)了緊急問題時。桌面應(yīng)用程序可以通過彈出通知來做到這一點,但腳本也可以做到這一點!你可以使用腳本命令來給自己發(fā)送桌面通知與提醒。
Example notification
下面的代碼是在 Linux 上編寫和測試的。它也可以在 macOS 上運行,只需花點功夫。請參見最后一節(jié) 提示與技巧。
從 Linux 終端發(fā)送通知
要從 Linux 終端發(fā)送通知,請使用 notify-send? 命令。運行 which notify-send 命令來查看它是否在于你的系統(tǒng)中。如果沒有,請使用包管理器來安裝它。
在 Fedora 上,輸入:
$ sudo dnf install notify-send
在基于 Debian 的發(fā)行版上,輸入:
$ sudo apt install notify-send
幾個簡單的通知示例:
$ notify-send "Dinner ready!"
$ notify-send "Tip of the Day" "How about a nap?"
你可以用緊急程度、自定義圖標等選項來自定義通知。過 man notify-send 了解更多。你也可以在通知正文中使用一小組 HTML 標記,以使消息有一個棒的視覺感受。最重要的是,URL 被呈現(xiàn)為可點擊的。例如:
$ notify-send -u critical \
"Build failed!" \
"There were <b>123</b> errors. Click here to see the results: http://buildserver/latest"
Build fail notification
發(fā)送的通知會被桌面環(huán)境接收,并像其他通知一樣顯示。它們將具有相同的外觀、交互和行為。
將 notify-send 與 at 結(jié)合使用
計劃任務(wù)通常被用來定期安排命令。at 命令安排在一個指定的時間執(zhí)行一條命令。如果你像這樣運行它,它會以交互模式啟動,你可以在其中輸入要在指定時間執(zhí)行的命令:
$ at 12:00
這對腳本來說并不有用。幸運的是 at 接受來自標準輸入的參數(shù),所以我們可以這樣使用它:
$ echo "npm run build" | at now + 1 minute
$ echo "backup-db" | at 13:00
有許多指定時間的方法。 從絕對時間,如 10:00?,到相對時間,如 now + 2 hours? ,再特殊時間,如noon? 或 midnight?。我們可以把它和 notify-send 結(jié)合起來,在未來的某個時間向自己發(fā)送提醒。例如:
$ echo "notify-send 'Stop it and go home now?' 'Enough work for today.' -u critical" | at now
Stop for the day notification
提醒的命令
現(xiàn)在,建立一個自定義的 Bash 命令來給自己發(fā)送提醒信息。像這樣簡單且人性化的命令:
$ 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
這比 Alexa 更好!該怎樣做?
請看下面的代碼。它定義了一個名為 remind 的函數(shù),它支持上述語法。實際工作是在最后兩行完成的。其余的部分負責(zé)顯示幫助信息、參數(shù)校驗等,這與任何大型應(yīng)用程序中有用的代碼與必要的白噪聲的比例大致相同。
把代碼保存在某個地方,例如,在 ~/bin/remind? 文件中,并在你的 .bashrc 配置文件寫入該函數(shù),以便在你登錄時加載它:
$ source ~/bin/remind
重新打開終端,然后輸入 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"
}
簡單的提醒
通過這幾個簡單的開源命令,你可以將你自己的腳本、應(yīng)用程序和任務(wù)與你的桌面結(jié)合起來。試一試吧!