-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessage_gui_user
61 lines (51 loc) · 1.93 KB
/
message_gui_user
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
56
57
58
59
60
61
#! /bin/bash
#----------------------------------------------------------------------
# Description: send a notification to a currently logged in user in X.org
# Author: Artem S. Tashkinov
# Created at: Thu Jan 16 22:57:14 2020
# It took me three hours to write and debug it though it looks simple
# Computer: zen
# System: Linux 5.4.8-az2 on x86_64
#
# Copyright (c) 2020 Artem S. Tashkinov All rights reserved.
#
#----------------------------------------------------------------------
msg_icon=/path/to/icon_32x32.png
test -z "$3" && echo "Usage is `basename "$0"` timeout(seconds) title message" && exit 1
timeout=$1
# message type modern/classic
mstype=modern
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "$1 is not an integer number."
exit 1
fi
whos() # print users whose sessions start with ":" i.e Xorg users
{
who | awk '{if ($2~/^:/) print}'
}
message() {
echo "Detected user:$1 DISPLAY=$2. Attempting to show a $mstype message ..."
if [ "$mstype" = "classic" ]; then
su "$1" -c "DISPLAY=$2 xmessage -center -timeout $timeout -title '$3' '$4'"
else
su "$1" -c "DISPLAY=$2 notify-send --expire-time="${timeout}000" --icon="$msg_icon" '$3' '$4'"
fi
}
pid_lxdm=`pidof lxdm-session`
test -n "$pid_lxdm" && Xuser=`ps --no-headers -u --ppid "$pid_lxdm" | awk '{print $1}'`
if [ -n "$Xuser" ]; then
# LXDM version
echo "LXDM session detected ..."
Xdisp=`ps --no-headers -u --ppid "$pid_lxdm" | awk '{print $2}'`
Xdisp=`tr '\0' '\n' < /proc/$Xdisp/environ | awk -F = '/^DISPLAY=/{print $2}'`
message "$Xuser" "$Xdisp" "$2 for ${timeout}sec" "$3"
elif [ -n "`whos`" ]; then
# Universal version in case w does work (it doesn't for LXDM)
echo "who is working :)"
whos | while read Xuser Xdisp; do
message "$Xuser" "$Xdisp" "$2 for ${timeout}sec" "$3"
done
else
echo "Attemptin to show a display login manager message ..."
message "root" ":0" "$2 for ${timeout}sec" "$3"
fi