Last active
February 20, 2022 19:28
-
-
Save DeivAstra/fbfa6b0ee9244d7b59cc01bbf8435fa1 to your computer and use it in GitHub Desktop.
Inactive desktop apps freezer (bash script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ### Info ### | |
| # This script freez inactive desktop applications to save CPU and bandwidth consumption. | |
| # When you open some window then script unfreeze the window back. | |
| # Apps wmctrl and xwininfo need installed before start this script. | |
| # List all windows by `wmctrl -l -x -p` to get string which you will be use in grep_query (see below). | |
| # Show window info by window id: xwininfo -id <window_id>. The command shows window states: IsViewable or IsUnMapped. | |
| # Modify grep_query variable to get which, for example, windows classes you want to freeze. You can use windows title or part also. | |
| # This script runs infinitely with timeout 3 sec. Press Ctrl-C or kill him. | |
| # You can freeze/unfreeze any process(if you have permissions) manually use `kill -SIGSTOP <pid>` and `kill -SIGCONT <pid>`. | |
| grep_query='chromium.Chromium\|Navigator.Pale moon\|Navigator.firefox' | |
| window_info_lines=`wmctrl -l -x -p | grep "$grep_query"` | |
| echo $window_info_lines | |
| function log { | |
| # echo "$1"# >> app-freezer.log | |
| # echo "$1" | |
| : | |
| } | |
| proc_windows_active=1 | |
| function set_proc_windows_active { | |
| proc_windows_active=0 | |
| local proc_id=$1 | |
| local proc_window_lines=`wmctrl -l -x -p | grep "$proc_id"` | |
| while read -r line | |
| do | |
| local win_id=`echo "$line" | cut -d" " -f1` | |
| local state=`xwininfo -id $win_id | grep "Map State" | cut -d" " -f5` | |
| if [ $state == "IsViewable" ]; then | |
| proc_windows_active=1 | |
| return | |
| fi | |
| done <<< "$proc_window_lines" | |
| } | |
| function process_windows { | |
| while read -r line | |
| do | |
| #log "$line" | |
| local win_id=`echo "$line" | cut -d" " -f1` | |
| local proc_id=`echo "$line" | cut -d" " -f4` | |
| log "win_id: $win_id, proc_id: $proc_id" | |
| local state=`xwininfo -id $win_id | grep "Map State" | cut -d" " -f5` | |
| #log "$state" | |
| set_proc_windows_active $proc_id | |
| if [ $proc_windows_active = 1 ]; then | |
| log "Unfreeze .." | |
| kill -SIGCONT $proc_id | |
| pkill -SIGCONT -P $proc_id | |
| else | |
| log "Freeze .." | |
| kill -SIGSTOP $proc_id | |
| pkill -SIGSTOP -P $proc_id | |
| fi | |
| #log "Done" | |
| done <<< "$window_info_lines" | |
| } | |
| trap ctrl_c INT | |
| function ctrl_c() { | |
| echo "Trapped CTRL-C. Unfreez all freezed processes.." | |
| proc_stopped_lines=`pgrep -rT` | |
| while read -r line | |
| do | |
| echo "Unfreez $line" | |
| kill -SIGCONT $line | |
| done <<< "$proc_stopped_lines" | |
| exit 0 | |
| } | |
| while true; do process_windows; sleep 3; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment