Here is a script I wrote to suspend some heavy processes like firefox-esr when screen is off to reduce power usage and excess heat. Doing a good job at keeping phone cool in pocket (along with 2min30sec suspend timeout) but would be a lot better if I could have it react to the screen being locked and a call in progress. Any ideas on how to monitor for these events?
#!/bin/bash
# Variable to store the previous screen status
prev_status=""
# Function to check screen status
check_screen_status() {
# Retrieve current screen status
screen_status=$(wlr-randr | grep -o "Enabled: yes")
# Compare current status with previous status
if [ "$screen_status" != "$prev_status" ]; then
if [ -n "$screen_status" ]; then
echo "Screen is ON"
#kill -CONT `pgrep -o Nheko` &
kill -CONT `pgrep -o firefox-esr` &
kill -CONT `pgrep -o armcord` &
#kill -CONT `pgrep -o gnome-software` &
kill -CONT `pgrep -o telegram` &
kill -CONT `pgrep -o moonlight ` &
else
echo "Screen is OFF"
#kill -STOP `pgrep -o Nheko`
kill -STOP `pgrep -o firefox-esr`
#kill -STOP `pgrep -o armcord`
#kill -STOP `pgrep -o telegram`
kill -STOP `pgrep -o gnome-software`
kill -STOP `pgrep -o moonlight`
# Run your command here when the screen turns off
# Example: notify-send "Screen turned off"
fi
# Update previous status
prev_status="$screen_status"
fi
}
# Main loop to continuously check screen status
while true; do
check_screen_status
sleep 1 # Adjust sleep interval as needed
done
Mind if I mod this script for my current setup?
Go for it! Posted it to share and maybe work together to improve it.
Thought of suspending Geary and some other programs so that they open in background when the screen becomes active.
You could use journalctl -u ModemManager -n 1 | grep -c 'accepted\|ringing-out') if I’m not mistaken.
I thought I’d share what this script has evolved into. I haven’t added the check for an active call but so far it doesn’t seem to need it.
I’ve added cgroups to to prevent swapping of important processes and to prevent swapping + restrict memory usage of firefox-esr which is a huge performance boost. It seems like as soon as firefox starts to swap it slows down a lot. It also seems like if any of the core/gnome/audio/ui processes have anything swapped things get a bit laggy. cgroups seem like the magic ticket to keep everything reliably smooth.
This script will require allowing some things in /etc/sudoers
I also created a shortcut to wake up firefox-esr on demand rather than every time the screen wakes to keep things cool and improves my standby time.
autosuspend.sh
#!/bin/bash
# Variable to store the previous screen status
prev_status=""
# Function to check screen status
check_screen_status() {
# Retrieve current screen status
screen_status=$(wlr-randr | grep -o "Enabled: yes")
# Compare current status with previous status
if [ "$screen_status" != "$prev_status" ]; then
if [ -n "$screen_status" ]; then
echo "Screen is ON"
echo "journalctl vacuum"
sudo journalctl --vacuum-size=100M
echo "Freeing RAM"
sudo sync
echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/null && echo "Dropped caches"
#pgrep firefox-esr && echo firefox-esr CONT && kill -CONT `pgrep -o firefox-esr`
pgrep armcord && echo armcord CONT && kill -CONT `pgrep -o armcord`
pgrep telegram && echo telegram CONT && kill -CONT `pgrep -o telegram`
pgrep moonlight && echo moonlight CONT && kill -CONT `pgrep -o moonlight`
pgrep Nheko && echo Nheko CONT && kill -CONT `pgrep -o Nheko`
sudo cpufreq-set -c 0,1,2,3 --max 1500000 && cpufreq-info | grep -m 1 "current policy" | awk -F'[ -]+' '{print $8, $11}'
else
echo "Screen is OFF"
pgrep moonlight && echo moonlight STOP && kill -STOP `pgrep -o moonlight`
pgrep Nheko && echo Nheko STOP && kill -STOP `pgrep -o Nheko`
echo "cpu-freq"
sudo cpufreq-set -c 0,1,2,3 --max 1000000 && cpufreq-info | grep -m 1 "current policy" | awk -F'[ -]+' '{print $8, $11}'
echo "cgroup/firefox"
sudo mkdir /sys/fs/cgroup/firefox
echo "500M" | sudo tee /sys/fs/cgroup/firefox/memory.max
echo "0" | sudo tee /sys/fs/cgroup/firefox/memory.swap.max
echo `pgrep firefox-esr` | sudo tee /sys/fs/cgroup/firefox/cgroup.procs
echo "cgroup/core"
sudo mkdir /sys/fs/cgroup/core
echo "0" | sudo tee /sys/fs/cgroup/core/memory.swap.max
#echo "pipewire" && echo `pgrep pipewire` | sudo tee /sys/fs/cgroup/core/cgroup.procs
#echo "pipewire-media-session" && echo `pgrep pipewire-media-session` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep pulseaudio` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep rtkit-daemon` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep gnome-calls` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep callaudiod` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep NetworkManager` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep usbguard-dbus` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep phoc` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep squeekboard` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep ssh` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep sshd` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep ssh-agent` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep autossh` | sudo tee /sys/fs/cgroup/core/cgroup.procs
echo `pgrep chatty` | sudo tee /sys/fs/cgroup/core/cgroup.procs
sqlite3 /home/purism/purple/chatty/db/chatty-history.db "VACUUM;"
echo "swap off/on"
sudo swapoff -a
sudo swapon -a
pgrep firefox-esr && echo firefox-esr STOP && kill -STOP `pgrep -o firefox-esr`
fi
# Update previous status
prev_status="$screen_status"
fi
}
# Main loop to continuously check screen status
while true; do
check_screen_status
sleep 1 # Adjust sleep interval as needed
done
manualwake.sh
#kill -CONT `pgrep -o firefox-esr` &
for pid in $(pgrep -f firefox); do kill -CONT $pid; done
/etc/sudoers
...
purism ALL=(ALL) NOPASSWD: /usr/bin/sync
purism ALL=(ALL) NOPASSWD: /usr/bin/tee
purism ALL=(ALL) NOPASSWD: /usr/bin/journalctl --vacuum-size=100M
purism ALL=(ALL) NOPASSWD: /usr/bin/mkdir /sys/fs/cgroup/firefox
purism ALL=(ALL) NOPASSWD: /usr/bin/mkdir /sys/fs/cgroup/core
purism ALL=(ALL) NOPASSWD: /usr/sbin/swapon -a
purism ALL=(ALL) NOPASSWD: /usr/sbin/swapoff -a
purism ALL=(ALL) NOPASSWD: /usr/bin/tee
purism ALL=(ALL) NOPASSWD: /usr/bin/cpufreq-set