Working script that suspends heavy processes when screen off but looking for event to suspend processes when locked or call in progress

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

5 Likes

Mind if I mod this script for my current setup?

1 Like

Go for it! Posted it to share and maybe work together to improve it.

3 Likes

Thought of suspending Geary and some other programs so that they open in background when the screen becomes active.

1 Like

You could use journalctl -u ModemManager -n 1 | grep -c 'accepted\|ringing-out') if Iā€™m not mistaken.

1 Like