从 Linux 终端发送桌面通知和提醒

本 Linux 教程演示了如何使用脚本命令向自己发送桌面通知和提醒。
49 位读者喜欢这篇文章。
Person using a laptop

有时,从脚本获得视觉反馈很有用。例如,当脚本或 cron 作业完成、长时间运行的构建失败或脚本执行期间出现紧急问题时。桌面应用程序可以使用弹出通知来做到这一点,但也可以从脚本中完成!您可以使用脚本命令向自己发送桌面通知和提醒。

Example notification

(Tomasz Waraksa, CC BY-SA 4.0)

以下代码已在 Linux 上编写和测试。它也可以在 macOS 上完成,但需要付出一些努力。请参阅最后一节,了解一些 提示和技巧

从 Linux 终端发送通知

要从 Linux 终端发送通知,请使用 notify-send 命令。它通常已经作为桌面的一部分安装,但您可以运行 which notify-send 来确认。如果尚未安装,请使用您选择的软件包管理器安装它。

在 Fedora 和类似发行版上,输入

$ sudo dnf install libnotify

在基于 Debian 的发行版上,输入

$ sudo apt install notify-send

一些简单的通知示例

$ notify-send "Dinner ready!"
$ notify-send "Tip of the Day" "How about a nap?"

您可以使用紧急级别、自定义图标等选项自定义通知。使用 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"

Build fail notification

(Tomasz Waraksa, CC BY-SA 4.0)

发送的通知会被桌面环境拾取,并像任何其他通知一样显示。它们将具有相同一致的外观、感觉和行为。

将 notify-send 与 at 结合使用

Cron 通常用于按固定时间间隔安排命令。at 命令在指定时间安排命令的单次执行。如果您像这样运行它,它会在交互模式下启动,您可以在其中输入要在给定时间执行的命令

$ at 12:00

这对脚本没有用。幸运的是,at 接受来自标准输入的参数,因此我们可以这样使用它

$ echo "npm run build" | at now + 1 minute
$ echo "backup-db" | at 13:00

有很多方法可以指定时间。从绝对时间(如 10:00)到相对时间(如 now + 2 hours),再到特殊时间(如 noonmidnight)。我们可以将其与 notify-send 结合使用,以便在未来的某个时间向自己显示提醒。例如

$ echo "notify-send 'Stop it and go home now?' 'Enough work for today.' -u critical" | at now

Stop for the day notification

(Tomasz Waraksa, CC BY-SA 4.0)

remind 命令

现在,构建一个自定义 Bash 命令,用于向自己发送提醒。像下面这样简单且人性化的怎么样?

$ 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 的 shell 函数,该函数支持上述语法。实际工作在最后两行完成。其余部分负责帮助、参数验证等,这大致符合任何大型应用程序中有用代码与必要的无用代码的比例。

将代码保存在某个地方,例如,在 ~/bin/remind 文件中,并在您的 .bashrc 配置文件中 source 该函数,以便在您登录时加载它

$ 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"
}

轻松通知

使用这几个简单的开源命令,您可以将自己的脚本、应用程序和任务与您的桌面集成。试试看!


本文经作者许可,改编自原始文章,可在 此处 找到。

标签

5 条评论

你好!
很棒的文章。工具的良好组合!! ?
但是我在一篇也在这里发布的相关文章 (https://open-source.net.cn/article/21/8/linux-at-command) 中说过,仅仅安装工具是不够的,守护进程必须运行才能使用 at 命令,这也是强制性的。

问候! ?

感谢您的访问和提示,我将在原始文章中添加此内容!

回复 ,作者是 victorhck

嗨!
感谢您的文章!
对于 Gentoo+Fluxbox,可能需要额外的步骤才能使您的函数工作
1) 创建文件,例如 notify_user
#!/usr/bin/env bash
cd "$HOME/.dbus/session-bus"
dbus_file="$( ls -t1 | head -n 1 )"
export DBUS_SESSION_BUS_ADDRESS="$( source "$dbus_file"; echo $DBUS_SESSION_BUS_ADDRESS )"
export DISPLAY=":0.0"
notify-send Reminder "$1" -u critical
#不要忘记使其可执行:chmod +x
#有关其他信息和讨论,请参阅 #https://forums.gentoo.org/viewtopic-t-956982-start-0.html
2) 在函数 remind 中更改行
echo "notify-send '$MESSAGE' 'Reminder' -u critical" | at $TIME 2>/dev/null
改为
echo "doas -u shat /anywhere/notify_user '$MESSAGE'" | at $TIME 2>/dev/null

感谢提示!我没有任何 Gentoo 经验,所以我确信这对 Gentoo 用户会有所帮助!

回复 ,作者是 AShat

如果您想要一个稍微轻松的版本,请替换行

echo "notify-send '$MESSAGE' 'Reminder' -u critical" | at $TIME 2>/dev/null

xcowsay ${MESSAGE} | at $TIME 2>/dev/null

整体效果相同,但带有在屏幕上随机点弹出的图形。

Creative Commons License本作品根据 Creative Commons Attribution-Share Alike 4.0 International License 获得许可。
© . All rights reserved.