-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbash.bash_logout
More file actions
79 lines (63 loc) · 3.16 KB
/
bash.bash_logout
File metadata and controls
79 lines (63 loc) · 3.16 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# ContainerSSH logout handler for user testing
# This script runs when a user logs out from their shell session
# ============================================================================
# Custom Logout Hooks - Add your extended functionality here
# ============================================================================
# Create log directory in persistent storage
log_dir="$HOME/.kube_logs"
mkdir -p "$log_dir"
# Log file path
log_file="$log_dir/logout.log"
# Get initial timestamp for start message
initial_utc_epoch=$(date -u +%s)
initial_kst_epoch=$((initial_utc_epoch + 32400))
initial_timestamp=$(date -d "@$initial_kst_epoch" '+%Y-%m-%d %H:%M:%S KST' 2>/dev/null) || initial_timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$initial_timestamp] start logout process" >> "$log_file"
# ============================================================================
# Custom function called before logout processing
on_farewell_custom() {
# Get timestamp inside function to ensure it's available
local utc_epoch=$(date -u +%s)
local kst_epoch=$((utc_epoch + 32400))
local timestamp=$(date -d "@$kst_epoch" '+%Y-%m-%d %H:%M:%S KST' 2>/dev/null) || timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local pod_name=$(hostname)
echo "[$timestamp] start farewell process" >> "$log_file"
# Add timeout and better error handling for curl
if curl -s --connect-timeout 10 --max-time 30 -X POST "http://210.94.179.19:9732/report-background" \
-H "Content-Type: application/json" \
-d "{\"username\": \"$USER\", \"pod_name\": \"$pod_name\"}" \
>> "$log_file" 2>&1; then
echo "[$timestamp] Report sent for user '$USER', pod '$pod_name'" >> "$log_file"
else
echo "[$timestamp] Failed to send report for user '$USER', pod '$pod_name'" >> "$log_file"
fi
return 0
}
# Get current timestamp in Korean time - simplified approach
utc_epoch=$(date -u +%s)
kst_epoch=$((utc_epoch + 32400))
timestamp=$(date -d "@$kst_epoch" '+%Y-%m-%d %H:%M:%S KST' 2>/dev/null) || timestamp=$(date '+%Y-%m-%d %H:%M:%S')
user=${USER:-"unknown"}
hostname=$(hostname 2>/dev/null || echo "unknown")
# Use original PID if available, otherwise current PID
actual_pid=${ORIGINAL_PID:-$$}
# Execute custom logout hook first
echo "[$timestamp] About to call on_farewell_custom" >> "$log_file"
on_farewell_custom
hook_result=$?
echo "[$timestamp] on_farewell_custom returned with code: $hook_result" >> "$log_file"
# Check if custom hook blocked the logout
if [ $hook_result -ne 0 ]; then
echo "[$timestamp] Logout blocked by custom hook (exit code: $hook_result)" >> "$log_file"
exit $hook_result # This will prevent shell termination if called in blocking mode
fi
# Write logout event to log file
echo "[$timestamp] User '$user' logged out from hostname '$hostname' (PID: $actual_pid)" >> "$log_file"
# Also log environment info for debugging
echo "[$timestamp] Environment: HOME=$HOME, PWD=$PWD, SHELL=$SHELL, Monitor_PID=${ORIGINAL_PID:-'not_set'}" >> "$log_file"
# Optional: Send notification or perform cleanup tasks
# echo "[$timestamp] Cleanup completed for user '$user'" >> "$log_file"
# Ensure log file is readable
chmod 644 "$log_file"
exit 0