Last active
July 19, 2025 05:25
-
-
Save felipealfonsog/1fc33730b0d975ca5f36d465356e1e99 to your computer and use it in GitHub Desktop.
This script keeps WiFi connected when the laptop lid is closed and auto-reconnects if disconnected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Ensure the script is run as root | |
if [[ "$EUID" -ne 0 ]]; then | |
echo "Please run as root" | |
exit 1 | |
fi | |
echo "1. Mark all WiFi services as autoconnect..." | |
for service in $(connmanctl services | awk '/^\*/{print $3}'); do | |
echo "Setting autoconnect for $service..." | |
connmanctl config "$service" --autoconnect yes | |
done | |
echo "2. Enabling connman service at boot..." | |
systemctl enable connman.service | |
systemctl start connman.service | |
echo "3. Prevent suspend on lid close..." | |
sed -i 's/^HandleLidSwitch=.*/HandleLidSwitch=ignore/' /etc/systemd/logind.conf | |
sed -i 's/^HandleLidSwitchDocked=.*/HandleLidSwitchDocked=ignore/' /etc/systemd/logind.conf | |
loginctl enable-linger $(logname) | |
echo "Restarting systemd-logind to apply lid config..." | |
systemctl restart systemd-logind | |
echo "4. Creating systemd service to auto-reconnect if disconnected..." | |
cat > /etc/systemd/system/wifi-reconnect.service <<EOF | |
[Unit] | |
Description=Ensure WiFi is connected | |
After=network.target | |
[Service] | |
Type=simple | |
ExecStart=/usr/local/bin/wifi-reconnect.sh | |
Restart=always | |
RestartSec=10 | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
cat > /usr/local/bin/wifi-reconnect.sh <<'EOB' | |
#!/bin/bash | |
while true; do | |
if ! connmanctl state | grep -q "State = online"; then | |
echo "WiFi not connected. Attempting reconnection..." | |
connmanctl enable wifi | |
connmanctl scan wifi | |
sleep 3 | |
for service in $(connmanctl services | awk '/^\*/{print $3}'); do | |
connmanctl connect "$service" | |
done | |
fi | |
sleep 15 | |
done | |
EOB | |
chmod +x /usr/local/bin/wifi-reconnect.sh | |
echo "5. Enabling wifi-reconnect service..." | |
systemctl enable wifi-reconnect.service | |
systemctl start wifi-reconnect.service | |
echo "✅ Done. Your device will stay connected to WiFi even with the lid closed and will auto-reconnect if needed." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Ensure the script is run as root | |
if [[ "$EUID" -ne 0 ]]; then | |
echo "Please run as root" | |
exit 1 | |
fi | |
echo "1. Mark all WiFi services as autoconnect..." | |
for service in $(connmanctl services | awk '/^\*/{print $3}'); do | |
echo "Setting autoconnect for $service..." | |
connmanctl config "$service" --autoconnect yes | |
done | |
echo "2. Enabling connman service at boot..." | |
systemctl enable --now connman.service | |
echo "3. Prevent suspend on lid close..." | |
CONFIG_FILE="/etc/systemd/logind.conf" | |
# Backup original config if not already backed up | |
[[ ! -f "${CONFIG_FILE}.bak" ]] && cp "$CONFIG_FILE" "${CONFIG_FILE}.bak" | |
# Replace or append lid settings | |
grep -q "^HandleLidSwitch=" "$CONFIG_FILE" && \ | |
sed -i 's/^HandleLidSwitch=.*/HandleLidSwitch=ignore/' "$CONFIG_FILE" || \ | |
echo "HandleLidSwitch=ignore" >> "$CONFIG_FILE" | |
grep -q "^HandleLidSwitchDocked=" "$CONFIG_FILE" && \ | |
sed -i 's/^HandleLidSwitchDocked=.*/HandleLidSwitchDocked=ignore/' "$CONFIG_FILE" || \ | |
echo "HandleLidSwitchDocked=ignore" >> "$CONFIG_FILE" | |
# Enable linger so systemd user services persist across suspend/reboot | |
loginctl enable-linger $(logname) | |
echo "Restarting systemd-logind to apply lid config..." | |
systemctl restart systemd-logind | |
echo "4. Creating systemd service to auto-reconnect if disconnected..." | |
cat > /etc/systemd/system/wifi-reconnect.service <<EOF | |
[Unit] | |
Description=Ensure WiFi is connected | |
After=network.target | |
[Service] | |
Type=simple | |
ExecStart=/usr/local/bin/wifi-reconnect.sh | |
Restart=always | |
RestartSec=10 | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
cat > /usr/local/bin/wifi-reconnect.sh <<'EOB' | |
#!/bin/bash | |
while true; do | |
if ! connmanctl state | grep -q "State = online"; then | |
echo "WiFi not connected. Attempting reconnection..." | |
connmanctl enable wifi | |
connmanctl scan wifi | |
sleep 3 | |
for service in $(connmanctl services | awk '/^\*/{print $3}'); do | |
connmanctl connect "$service" | |
done | |
fi | |
sleep 15 | |
done | |
EOB | |
chmod +x /usr/local/bin/wifi-reconnect.sh | |
echo "5. Enabling wifi-reconnect service..." | |
systemctl enable --now wifi-reconnect.service | |
echo "✅ Setup complete. Your MacBook running Arch will stay connected to WiFi with the lid closed and auto-reconnect if disconnected." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment