-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisable_lid_sleep.sh
executable file
·40 lines (35 loc) · 1.05 KB
/
disable_lid_sleep.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
#!/bin/bash
# Function to check if script is run as root
check_root() {
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root" >&2
exit 1
fi
}
# Function to append HandleLidSwitch=ignore to /etc/logind.conf
add_handle_lid_switch() {
if grep -q "^HandleLidSwitch=" /etc/systemd/logind.conf; then
echo "HandleLidSwitch is already defined in /etc/logind.conf"
else
echo "HandleLidSwitch=ignore" >> /etc/systemd/logind.conf
echo "HandleLidSwitch=ignore added to /etc/systemd/logind.conf"
fi
}
# Main function
main() {
if ! check_root; then
# If not root, ask for sudo authentication
if sudo -v; then
# If sudo authentication successful, call function to append HandleLidSwitch=ignore
add_handle_lid_switch
else
echo "Authentication failed, aborting script" >&2
exit 1
fi
else
# If root, directly call function to append HandleLidSwitch=ignore
add_handle_lid_switch
fi
}
# Call main function
main