是否可以取消或清除使用通知发送创建的通知?

eva*_*vid 9 notification bash scripts notify-osd notify-send

所以我写了一个小脚本来确保某个用户在登录时插入笔记本电脑(如果他没有,它会禁用)。脚本使用notify-send 告诉他插入。如果他插入,脚本将退出。他插上的时候能不能自动清除通知?我认为它可能需要以某种方式获取通过通知发送生成的进程 ID 并杀死该 PID,但我不知道如何执行此操作。

这是当前的脚本:

#!/bin/bash 

cat /sys/class/power_supply/BAT0/status
OUTPUT="$(cat /sys/class/power_supply/BAT0/status)"
echo "${OUTPUT}"
if [ "${OUTPUT}" = "Charging" ] || [ "${OUTPUT}" = "Unknown" ]; then
    echo charging or full
elif [ "${OUTPUT}" = "Discharging" ]; then
    notify-send -i /home/evamvid/Documents/Programming/OokiNoUse/power25.png "Hey there brother" "plug it in"
    COUNTER=0
while [ "$COUNTER" -le 12 ]
do
    cat /sys/class/power_supply/BAT0/status
    OUTPUT="$(cat /sys/class/power_supply/BAT0/status)"
    echo "${OUTPUT}"
    if [ "${OUTPUT}" = "Charging" ] || [ "${OUTPUT}" = "Unknown" ]; then
        exit
    elif [ "${OUTPUT}" = "Discharging" ]; then
        COUNTER=$(($COUNTER+1))
        echo $COUNTER
        sleep 1
    fi
done
fi
Run Code Online (Sandbox Code Playgroud)

agg*_*877 12

或者,如果您根本不希望通知显示在通知历史记录中,您可以尝试:

notify-send --hint int:transient:1 "Title" "Body"
Run Code Online (Sandbox Code Playgroud)


Jac*_*ijm 7

您正在寻找的过程是notify-osd. 您可以通过以下任一命令杀死它:

pkill notify-osd
Run Code Online (Sandbox Code Playgroud)

或者通过它的pid:

kill $(ps -e | grep notify-osd | awk '{ print $1 }')
Run Code Online (Sandbox Code Playgroud)

或者,甚至更好,正如@kos 所建议的(谢谢!),使用pgrep

kill $(pgrep ^notify-osd$)
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,使用 xfce 时,进程没有命名为 `notify-osd`,而是命名为 `xfce4-notifyd`,更具体地说是 `/usr/lib/x86_64-linux-gnu/xfce4/notifyd/xfce4-notifyd`。我可以改为执行`pkill xfce4-notifyd`,它会杀死现有的通知。下一次 notify-send 运行时,它会再次自动创建 xfce4-notifyd 进程,因此看起来很安全。 (2认同)