-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpowersave.sh
132 lines (112 loc) · 3.68 KB
/
powersave.sh
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
#!/bin/bash
# Function to set power save for a given interface
set_power_save() {
local interface=$1
local state=$2
echo "Setting power save to $state for $interface..."
if sudo iw dev $interface set power_save $state; then
echo "Power save set to $state for $interface using iw command."
else
echo "Failed to set power save using iw command. Please check your permissions or wireless interface status."
return 1
fi
}
# Function to get and display power save status
get_power_save_status() {
local interface=$1
local status=$(iw dev $interface get power_save | awk '{print $3}')
echo -n "$interface: Power save: "
if [ "$status" = "on" ]; then
echo "on"
elif [ "$status" = "off" ]; then
echo "off"
else
echo "unknown (raw output: $status)"
fi
}
# Function to make changes persistent
make_persistent() {
local state=$1
echo "Making power save settings persistent..."
# Create a script to be executed by the service
cat << EOF | sudo tee /usr/local/bin/set-wifi-power-save.sh > /dev/null
#!/bin/bash
sleep 10 # Wait for network interfaces to be fully up
for interface in \$(iw dev | awk '\$1=="Interface"{print \$2}'); do
iw dev \$interface set power_save $state
echo "Set power_save $state for \$interface"
done
EOF
sudo chmod +x /usr/local/bin/set-wifi-power-save.sh
# Create a systemd service file
cat << EOF | sudo tee /etc/systemd/system/wifi-power-save.service > /dev/null
[Unit]
Description=Set WiFi Power Save
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/set-wifi-power-save.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd, enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable wifi-power-save.service
sudo systemctl start wifi-power-save.service
echo "Persistent service created and enabled."
}
# Function to install a package if not installed
install_if_needed() {
local package=$1
if ! command -v $package &> /dev/null; then
echo "$package not found, installing..."
if [ -f /etc/fedora-release ]; then
sudo dnf install -y $package
elif [ -f /etc/lsb-release ]; then
sudo apt-get update
sudo apt-get install -y $package
else
echo "Unsupported Linux distribution. Please install $package manually."
exit 1
fi
else
echo "$package is already installed."
fi
}
# Install iw and lshw if necessary
install_if_needed iw
install_if_needed lshw
clear
# Get all wireless interfaces
wireless_interfaces=$(iw dev | awk '$1=="Interface"{print $2}')
if [ -z "$wireless_interfaces" ]; then
echo "No wireless interfaces found."
exit 1
fi
# Prompt user for power save state
echo "Choose power save state:"
echo "1) On"
echo "2) Off"
read -p "Enter your choice (1 or 2): " choice
case $choice in
1) state="on" ;;
2) state="off" ;;
*) echo "Invalid choice. Exiting."; exit 1 ;;
esac
# Set power save for each wireless interface
for interface in $wireless_interfaces; do
set_power_save $interface $state
done
# Verify power save status
echo -e "\nVerifying power save status:"
for interface in $wireless_interfaces; do
get_power_save_status $interface
done
# Make changes persistent
make_persistent $state
echo -e "\nPower save settings applied and made persistent."
echo "Changes should persist across reboots."
echo "You can check the status of the persistent service with: sudo systemctl status wifi-power-save.service"
echo "If issues persist, check the system logs with: sudo journalctl -u wifi-power-save.service"