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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>subh.space</title>
<style>
:root {
--bg0: #282828;
--bg1: #3c3836;
--fg: #ebdbb2;
--gray: #928374;
--yellow: #fabd2f;
--green: #b8bb26;
--orange: #fe8019;
--aqua: #8ec07c;
}
body {
font-family: 'Iosevka Nerd Font Propo';
line-height: 1.7;
color: var(--fg);
background-color: var(--bg0);
max-width: 780px;
margin: 40px auto;
padding: 0 20px;
-webkit-font-smoothing: antialiased;
}
h1 {
font-size: 2.2em;
color: var(--yellow);
border-bottom: 2px solid var(--bg1);
padding-bottom: 15px;
margin-bottom: 30px;
}
h2 {
font-size: 1.5em;
color: var(--aqua);
margin-top: 35px;
margin-bottom: 15px;
font-weight: 600;
}
p { margin-bottom: 1.2em; }
code {
font-family: 'Fira Code', 'JetBrains Mono', 'Courier New', monospace;
background-color: var(--bg1);
color: var(--orange);
padding: 3px 6px;
border-radius: 4px;
font-size: 0.9em;
}
pre {
background-color: #1d2021;
padding: 20px;
border-radius: 8px;
overflow-x: auto;
border: 1px solid var(--bg1);
margin-bottom: 1.5em;
}
pre code {
background-color: transparent;
padding: 0;
color: var(--fg);
color-scheme: dark;
}
ol, ul { margin-bottom: 1.5em; padding-left: 25px; }
li { margin-bottom: 0.8em; }
li pre { margin-top: 10px; margin-bottom: 10px; }
</style>
</head>
<body>
<h1>Configuring USB notifications on Linux</h1>
<p>If you want to receive a small notification as soon as a USB storage device is plugged in or plugged out. You're in the right place</p>
<p>Your system detects a USB device upon connection or disconnection using an utility named <code>udev</code>. udev allows for defining rules which can allow a user to perform a specific task when a USB device is connected or disconnected. This can be combined with <code>notify-send</code> to send notifications to a user as soon as a USB device is connected or disconnected</p>
<h2>1. Creating a udev rule</h2>
<p>Below is a rule file, which will trigger two scripts depending upon the connection or disconnection of a USB device</p>
<pre><code class="language-shell">ACTION=="add", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", RUN+="/usr/local/bin/usb-notify-add.sh '$env{ID_MODEL}'"
ACTION=="remove", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", RUN+="/usr/local/bin/usb-notify-rem.sh '$env{ID_MODEL}'"
</code></pre>
<p>save this as <code>99-usb-notify.rules</code> file under <code>/etc/udev/rules.d/</code>.</p>
<h2>2. Configuring the scripts</h2>
<p>Below are the scripts:</p>
<ul>
<li>usb-notify-add.sh</li>
</ul>
<pre><code class="language-shell">#!/bin/bash
DEVICE_NAME=${1:-"Unknown USB Device"}
USER_ID=$(id -u <your-username>)
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$USER_ID/bus
sudo -u <your-username> DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS /usr/bin/notify-send "USB Connected" "Device: $DEVICE_NAME"
</code></pre>
<ul>
<li>usb-notify-rem.sh</li>
</ul>
<pre><code class="language-shell">#!/bin/bash
DEVICE_NAME=${1:-"Unknown USB Device"}
USER_ID=$(id -u <your-username>)
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$USER_ID/bus
sudo -u <your-username> DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS /usr/bin/notify-send "USB Disconnected" "Device: $DEVICE_NAME"
</code></pre>
<p>Save both the scripts under <code>/usr/local/bin</code> and make them executable with <code>chmod</code></p>
<h2>3. Reloading the udev rules</h2>
<p>Once all the files have been saved. Reload udev rules as follows:</p>
<pre><code class="language-shell">sudo udevadm control --reload-rules
sudo udevadm trigger
</code></pre>
<p>And just like that, you've setup USB notifications. Give it a try! Plug in a USB device and check whether your system sends your a notification with the name of the USB device.</p>
</body>
</html>
|