Skip to content

Instantly share code, notes, and snippets.

@catb0t
Last active September 4, 2025 16:30
Show Gist options
  • Select an option

  • Save catb0t/fb2541ac812ab07024da9abded6f4a3e to your computer and use it in GitHub Desktop.

Select an option

Save catb0t/fb2541ac812ab07024da9abded6f4a3e to your computer and use it in GitHub Desktop.
automatically cpulimit heavy apps on battery power, like Floorp (Firefox) and Pulsar (Atom)
## as root, create mode 0644 /etc/acpi/events/cpulimit-apps
event=ac_adapter
action=/etc/acpi/actions/cpulimit-apps.bash %e
#!/bin/bash
## as root, create mode 0755 /etc/acpi/actions/cpulimit-apps.bash
shopt -s globstar
shopt -s extglob
# Refer to https://wiki.archlinux.org/title/Acpid
my_id="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)"
floorp_cpid="/tmp/pid-cpulimit-floorp"
pulsar_cpid="/tmp/pid-cpulimit-pulsar"
my_floorp_cpid="$floorp_cpid-$my_id"
my_pulsar_cpid="$pulsar_cpid-$my_id"
# $4 is the 4th field in the output of `acpi_listen`
# for ac_adapter on my laptop, it is exactly 00000001 when the adapter is plugged in
# and exactly 00000000 when the adapter is unplugged
# use `acpi_listen` to find the right event/value for your machine
case "$4" in
00000000)
cpulimit -P /app/lib/floorp/floorp -l 16 &
floorp_pid=$!
echo $floorp_pid > "$my_floorp_cpid"
cpulimit -P /usr/bin/pulsar -l 13 &
pulsar_pid=$!
echo $pulsar_pid > "$my_pulsar_cpid"
systemd-cat -p info -t acpid-actions-cpulimit-apps <<-EOF
acpid: cpulimit-apps: ac_adapter: cpulimits $floorp_pid and $pulsar_pid started and created related files in tmp: "$my_floorp_cpid" "$my_pulsar_cpid"
EOF
;;
00000001)
for id_file in "$floorp_cpid"-* "$pulsar_cpid"-*
do
this_pid="$(cat "$id_file")"
kill "$this_pid"
rm "$id_file"
systemd-cat -p info -t acpid-actions-cpulimit-apps <<-EOF
acpid: cpulimit-apps: ac_adapter: killed "$this_pid" and removed related file "$id_file"
EOF
done
;;
*) systemd-cat -p info -t acpid-actions-cpulimit-apps <<-EOF
acpid: cpulimit-apps: ac_adapter: ACPI action undefined for cpulimit-apps: $4
EOF
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment