-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewUserMonitor.sh
More file actions
58 lines (47 loc) · 1.5 KB
/
Copy pathNewUserMonitor.sh
File metadata and controls
58 lines (47 loc) · 1.5 KB
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
#!/bin/bash
import subprocess
import json
import time
from datetime import datetime
# Configuration
USERNAME_TO_MONITOR="bob"
STATUS_FILE = "/usr/local/bin/user_status.json"
CHECK_INTERVAL=3 # seconds
# Track previous state
user_existed=true
# Set up environment for GUI notifications
export DISPLAY=:0
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
function alert_user_deleted {
notify-send "User Deleted" "The user '$USERNAME_TO_MONITOR' has been removed from the system."
# Play a sound
if command -v paplay &> /dev/null; then
paplay /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga
elif command -v aplay &> /dev/null; then
aplay /usr/share/sounds/alsa/Front_Center.wav
else
echo -e "\a"
fi
}
# Get current timestamp in ISO format
LAST_CHECKED=$(date --iso-8601=seconds)
# Create JSON and write to file using jq
while true; do
if id "$USERNAME_TO_MONITOR" &>/dev/null; then
# User exists now
if ! $user_existed; then
echo "[INFO] User '$USERNAME_TO_MONITOR' recreated at $(date)"
fi
user_existed=true
else
# User does not exist now
if $user_existed; then
echo "[ALERT] User '$USERNAME_TO_MONITOR' deleted at $(date)"
alert_user_deleted
jq -n --arg status "$user_existed" --arg time "$LAST_CHECKED" \
'{User status: $user_existed, last_checked: $time}' > "$STATUS_FILE"
fi
user_existed=false
fi
sleep "$CHECK_INTERVAL"
done