-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification-queue-notifier
executable file
·56 lines (48 loc) · 2.05 KB
/
notification-queue-notifier
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bash
#
# Any program can send notifications by just writing to a queue file, e.g.
#
# $ do-stuff | other-stuff >> $HOME/.cache/notification-queue
#
# Notifications can be deferred in the sense that they will be held in the queue until the target
# user starts the process to read them. This means that the user can receive notifications from
# stuff that happened even before logging in.
#
# Any process from any user can send notifications as long as they have permission to write to the
# user queue file.
#
# The script runs forever reading the lines in the queue and showing the notifications.
#
# NOTE 1: This script should be started on user desktop login via platform-specific mechanisms,
# e.g. FreeDesktop (aka XDG) Autostart spec on Linux.
#
# NOTE 2: `notify-desktop` is my own script to run cross-platform notifications. This allows me to
# use this notification-queue system in any system supporting UNIX pipe files (should probably
# work on Windows/WSL, but not tested). If you don't need this, simply use `notify-send` on Linux
# from libnotify package (probably installed by default), or any tool for your OS.
# See https://github.com/gerardbosch/dotfiles/blob/cbd9d92224b0fdf5516e24e68aa97760d470a049/home/bin/notify-desktop
#
set -euo pipefail
# A FIFO pipe file (not a regular file)
QUEUE=$HOME/.cache/notification-queue
checkSingletonOrDie() {
# pgrep is limited to 15-char command name; -f looks for whole command line (may lead for false positives)
# $$ used to exclude the PID of self instance
if pgrep -f "$(basename "$0")" | grep -v "^$$\$" >/dev/null; then
echo "$(basename "$0")"
echo "Script is already running. Exiting."
exit 1
fi
}
# == Main ==
checkSingletonOrDie
basedir=$(dirname "$QUEUE")
test -d "$basedir" || mkdir -p "$basedir"
test -e "$QUEUE" || mkfifo "$QUEUE"
while [[ -e "$QUEUE" ]]; do
while read line; do
#notify-send "[Notification queue]" "$line" # <-- platform-specific (Linux)
notify-desktop "[Notification queue]" "$line" # <-- platform-independent
sleep 10
done < $QUEUE
done