Jade Dungeon

桌面信息提醒

notify-send

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

你也可以在通知正文中使用一小组 HTML 标记,URL 可点击。例如:

notify-send -u critical \
    "Build failed!" \
    "There were <b>123</b> errors. Click here to see the results: http://buildserver/latest"

将 notify-send 与 at 结合使用

计划任务通常被用来定期安排命令。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

创建自己的工具

现在,建立一个自定义的 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

定义一个名为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 &lt;message&gt; &lt;time&gt;"
    echo "    remind &lt;command&gt;"
    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 &gt;/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&gt;/dev/null
  echo "Notification scheduled at $TIME"
}

实际工作是在最后两行完成的。其余的部分负责显示帮助信息、参数校验等, 这与任何大型应用程序中有用的代码与必要的白噪声的比例大致相同。

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

$ source ~/bin/remind