Repository navigation
Expand file tree
/
Copy pathbootstrap_wol_sleep.sh
More file actions
executable file
·108 lines (88 loc) · 3.35 KB
/
Copy pathbootstrap_wol_sleep.sh
File metadata and controls
executable file
·108 lines (88 loc) · 3.35 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
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
#!/usr/bin/env bash
# ==============================================================================
# Dotfiles Setup Script: Wake-on-LAN Setup (Fedora)
# ==============================================================================
set -euo pipefail
# Configuration variables
SSH_PORT="22"
IDLE_THRESHOLD_MINUTES=30
echo "[+] Step 1: Enabling Wake-on-LAN in NetworkManager..."
ETH_CONN=$(nmcli -t -f NAME,TYPE connection show --active | grep ethernet | head -n1 | cut -d: -f1 || true)
if [ -n "$ETH_CONN" ]; then
echo " Found active Ethernet connection: '$ETH_CONN'"
sudo nmcli connection modify "$ETH_CONN" 802-3-ethernet.wake-on-lan magic
echo " Wake-on-LAN (magic packet) enabled for '$ETH_CONN'."
else
echo " [!] Warning: No active wired Ethernet connection detected via nmcli."
echo " Please enable WoL manually on your Ethernet interface once connected."
fi
echo "[+] Step 2: Creating idle monitor script (/usr/local/bin/check_idle.sh)..."
sudo tee /usr/local/bin/check_idle.sh > /dev/null << 'EOF'
#!/usr/bin/env bash
set -euo pipefail
# Service ports to check for active connections
SSH_PORT="__SSH_PORT__"
IDLE_LIMIT="__IDLE_LIMIT__"
STATE_FILE="/tmp/machine_idle_minutes"
# 1. Physical Keyboard Check
if ls /dev/input/by-id/*-kbd 1>/dev/null 2>&1; then
echo 0 > "$STATE_FILE"
exit 0
fi
# 2. CPU Load Check (< 10% total capacity across all cores)
LOAD_1MIN=$(awk '{print $1}' /proc/loadavg)
CORES=$(nproc)
THRESHOLD=$(awk -v cores="$CORES" 'BEGIN {print cores * 0.10}')
IS_BUSY=$(awk -v ld="$LOAD_1MIN" -v thresh="$THRESHOLD" 'BEGIN {print (ld > thresh) ? 1 : 0}')
if [ "$IS_BUSY" -eq 1 ]; then
echo 0 > "$STATE_FILE"
exit 0
fi
# 3. Target Network Traffic Check (SSH & HTTP endpoint)
# Ignores Tailscale daemon, DERP relays, and general system background pings
ACTIVE_CONNS=$(/usr/bin/ss -t -H state established "( sport = :${SSH_PORT} )" | wc -l)
if [ "$ACTIVE_CONNS" -gt 0 ]; then
echo 0 > "$STATE_FILE"
exit 0
fi
# Increment idle counter
IDLE_COUNT=$(cat "$STATE_FILE" 2>/dev/null || echo 0)
IDLE_COUNT=$((IDLE_COUNT + 1))
echo "$IDLE_COUNT" > "$STATE_FILE"
# Suspend if idle condition met continuously
if [ "$IDLE_COUNT" -ge "$IDLE_LIMIT" ]; then
echo 0 > "$STATE_FILE"
systemctl suspend
fi
EOF
# Inject variables into the created script
sudo sed -i "s/__SSH_PORT__/$SSH_PORT/g" /usr/local/bin/check_idle.sh
sudo sed -i "s/__IDLE_LIMIT__/$IDLE_THRESHOLD_MINUTES/g" /usr/local/bin/check_idle.sh
sudo chmod +x /usr/local/bin/check_idle.sh
echo "[+] Step 3: Setting up systemd timer for minute-by-minute checks..."
# Create systemd Service
sudo tee /etc/systemd/system/machine-idle-check.service > /dev/null << 'EOF'
[Unit]
Description=Machine System Idle Check
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/check_idle.sh
EOF
# Create systemd Timer (runs every minute)
sudo tee /etc/systemd/system/machine-idle-check.timer > /dev/null << 'EOF'
[Unit]
Description=Run Machine Idle Check Every Minute
[Timer]
OnCalendar=*:*
AccuracySec=1s
Persistent=true
[Install]
WantedBy=timers.target
EOF
echo "[+] Step 4: Reloading systemd and activating timer..."
sudo systemctl daemon-reload
sudo systemctl enable --now machine-idle-check.timer
echo "[✔] Machine idle setup complete!"
echo " Check status: systemctl status machine-idle-check.timer"
echo " View logs: journalctl -u machine-idle-check.service"