-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_recommended_power_settings.sh
executable file
·75 lines (62 loc) · 2.63 KB
/
create_recommended_power_settings.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
#!/bin/bash
set -eu # Abort on errors and unset variables
IFS=$(printf '\n\t') # IFS is only newline and tab
# Create a shell script to automatically apply powertop's recommened settings
# Copyright (C) 2016 Jesse McGraw (jlmcgraw@gmail.com)
#
#-------------------------------------------------------------------------------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
#-------------------------------------------------------------------------------
# Text formatting codes
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
# The name of the created script file
shell_script_file="./apply_recommended_power_settings.sh"
# Create the powertop csv output, outputs to "powertop.csv" by default
sudo powertop --csv --time=1
# Create the base shell script file to apply recommended settings
# <<- lets the heredoc ignore leading tabs so we can indent here for clarity
# tick-marks around EOF mean don't interpret variables
cat <<- 'EOF' > "$shell_script_file"
#!/bin/bash
set -eu # Abort on errors and unset variables
IFS=$(printf '\n\t') # IFS is only newline and tab
# Quit if we aren't root
if [ ${UID} -ne 0 ]
then
echo -e "Root privileges are needed to modify power settings\n"
exit 1
fi
EOF
# Make the new script executable
chmod +x "$shell_script_file"
# Cut out the "recommended settings" section from CSV output using perl
# Remove first and last lines using sed
# Add column 1 as comment and column 2 as command to output shell script
# using perl
cat powertop.csv |
perl -wlne '/^ Description [,;] Script/ix .. /^ [_]+ $/x and print' |
sed '1d;$d' |
perl -wlne '
if (/ ^ (.*?) [,;] (.*?) [,;] $/x) {
# The key is the comment, the value is the script
$values{$1} = $2;
}
END {
foreach $key (sort keys %values) {
print "# $key";
print "$values{$key}\n";
}
}
' >> "$shell_script_file"
# Quick advice on how to make the recommended settings take effect
echo "execute ${BOLD}sudo $shell_script_file${NORMAL} to make powertop's recommended settings take effect"