Guest User

taskbar_cpu_usage.sh

a guest
Jul 28th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.95 KB | None | 0 0
  1. #!/bin/bash
  2. ICON_CACHE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/simple_cpu_monitor_icon_cache"
  3. mkdir -p "$(dirname "$ICON_CACHE_FILE")"
  4. build_icon_cache() {
  5.     local cache_age=0
  6.     local newest_desktop=0
  7.     if [[ -f "$ICON_CACHE_FILE" ]]; then
  8.         cache_age=$(stat -c %Y "$ICON_CACHE_FILE")
  9.         newest_desktop=$(find ~/.local/share/applications /usr/share/applications -name '*.desktop' -printf '%T@\n' 2>/dev/null | sort -nr | head -n1 | cut -d. -f1)
  10.     fi
  11.     if [[ ! -f "$ICON_CACHE_FILE" || $newest_desktop -gt $cache_age ]]; then
  12.         > "$ICON_CACHE_FILE"
  13.         find ~/.local/share/applications /usr/share/applications -name '*.desktop' -print0 2>/dev/null | while IFS= read -r -d '' file; do
  14.             exec_line=$(grep -m1 '^Exec=' "$file" | cut -d= -f2 | awk '{print $1}')
  15.             icon_line=$(grep -m1 '^Icon=' "$file" | cut -d= -f2)
  16.             if [[ -n "$exec_line" && -n "$icon_line" ]]; then
  17.                 echo "$exec_line|$icon_line" >> "$ICON_CACHE_FILE"
  18.                 exec_base=$(basename "$exec_line")
  19.                 if [[ "$exec_base" != "$exec_line" ]]; then
  20.                     echo "$exec_base|$icon_line" >> "$ICON_CACHE_FILE"
  21.                 fi
  22.             fi
  23.         done
  24.     fi
  25. }
  26. get_icon_for_process() {
  27.     local proc="$1"
  28.     [[ "$proc" == "chrome" ]] && proc="google-chrome-stable"
  29.     local icon
  30.     icon=$(grep -m1 "^$proc|" "$ICON_CACHE_FILE" | cut -d'|' -f2)
  31.     if [[ -n "$icon" ]]; then
  32.         echo "$icon"
  33.     else
  34.         echo "$proc"
  35.     fi
  36. }
  37. build_icon_cache
  38. if [[ "$1" == "top_hungry_processes" ]]; then
  39.     tooltip_opt_n_procs=$(grep -m1 '^maxprocesses_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)
  40.     tooltip_opt_user_only="$(grep -m1 '^userprocessesonly_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
  41.     tooltop_opt_high_cpu="$(grep -m1 '^highcpu_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
  42.     tooltip_opt_cpu_alert=$(grep -m1 '^cpu_alert_value_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)
  43.     tooltip_opt_show_mem_procs="$(grep -m1 '^showmem_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
  44.     tooltop_opt_high_mem="$(grep -m1 '^highmem_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
  45.     tooltip_processes_icons_on_off="$(grep -m1 '^processesIcons=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
  46.     tooltip_opt_mem_alert=$(grep -m1 '^mem_alert_value_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)
  47.     tooltip_opt_hide_no_cpu_use_procs="$(grep -m1 '^hidezerocpu_tooltip=' ~/.config/plasma-org.kde.plasma.desktop-appletsrc | cut -d= -f2)"
  48.     if [[ -z $tooltip_opt_n_procs ]]; then
  49.         tooltip_opt_n_procs=5
  50.     fi
  51.     if [[ -z $tooltip_opt_mem_alert ]]; then
  52.         tooltip_opt_mem_alert=650
  53.     fi
  54.     if [[ -z $tooltip_opt_cpu_alert ]]; then
  55.         tooltip_opt_cpu_alert=20
  56.     fi
  57.     print_with_icon() {
  58.         local comm="$1"
  59.         local rest="$2"
  60.         if [[ -n "$tooltip_processes_icons_on_off" ]]; then
  61.             echo "icon_off | $rest"
  62.         else
  63.             local icon
  64.             icon=$(get_icon_for_process "$comm")
  65.             echo "$icon | $rest"
  66.         fi
  67.     }
  68.     if [[ -z "$tooltip_opt_user_only" && -n "$tooltop_opt_high_cpu" && -n "$tooltip_opt_show_mem_procs" ]]; then
  69.         ps -u "$USER" -o pid,comm,%cpu,rss --no-headers --sort=-%cpu | \
  70.         awk -v mem_alert="$tooltip_opt_mem_alert" -v cpu_alert="$tooltip_opt_cpu_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" -v high_mem_flag="$tooltop_opt_high_mem" '
  71.            $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
  72.                mem_mb = int($4/1024);
  73.                icon_cpu = ($3 > cpu_alert) ? "🧠" : "";
  74.                icon_mem = (high_mem_flag && mem_mb > mem_alert) ? "💾" : "";
  75.                if(icon_mem != "" && icon_cpu != "") {
  76.                    printf "%s|%s (%s%d%% - %s%dMB)\n", $2, $2, icon_cpu, int($3), icon_mem, mem_mb
  77.                } else if(icon_mem != "") {
  78.                    printf "%s|%s (%d%% - %s%dMB)\n", $2, $2, int($3), icon_mem, mem_mb
  79.                } else if(icon_cpu != "") {
  80.                    printf "%s|%s (%s%d%% - %dMB)\n", $2, $2, icon_cpu, int($3), mem_mb
  81.                } else {
  82.                    printf "%s|%s (%d%% - %dMB)\n", $2, $2, int($3), mem_mb
  83.                }
  84.            }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
  85.         exit
  86.     fi
  87.     if [[ -z "$tooltip_opt_user_only" && -n "$tooltop_opt_high_cpu" && -z "$tooltip_opt_show_mem_procs" ]]; then
  88.         ps -u "$USER" -o pid,comm,%cpu --no-headers --sort=-%cpu | \
  89.         awk -v cpu_alert="$tooltip_opt_cpu_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" '
  90.            $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
  91.                icon = ($3 > cpu_alert) ? "🧠" : "";
  92.                if(icon != "") {
  93.                    printf "%s|%s (%s%d%%)\n", $2, $2, icon, int($3)
  94.                } else {
  95.                    printf "%s|%s (%d%%)\n", $2, $2, int($3)
  96.                }
  97.            }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
  98.         exit
  99.     fi
  100.     if [[ -z "$tooltip_opt_user_only" && -z "$tooltop_opt_high_cpu" && -n "$tooltip_opt_show_mem_procs" ]]; then
  101.         ps -u "$USER" -o pid,comm,%cpu,rss --no-headers --sort=-%cpu | \
  102.         awk -v mem_alert="$tooltip_opt_mem_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" -v high_mem_flag="$tooltop_opt_high_mem" '
  103.            $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
  104.                mem_mb = int($4/1024);
  105.                icon_mem = (high_mem_flag && mem_mb > mem_alert) ? "💾" : "";
  106.                if(icon_mem != "") {
  107.                    printf "%s|%s (%d%% - %s%dMB)\n", $2, $2, int($3), icon_mem, mem_mb
  108.                } else {
  109.                    printf "%s|%s (%d%% - %dMB)\n", $2, $2, int($3), mem_mb
  110.                }
  111.            }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
  112.         exit
  113.     fi
  114.     if [[ -z "$tooltip_opt_user_only" && -z "$tooltop_opt_high_cpu" && -z "$tooltip_opt_show_mem_procs" ]]; then
  115.         ps -u "$USER" -o pid,comm,%cpu --no-headers --sort=-%cpu | \
  116.         awk -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" '
  117.            $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
  118.                printf "%s|%s (%d%%)\n", $2, $2, int($3)
  119.            }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
  120.         exit
  121.     fi
  122.     if [[ -n "$tooltip_opt_user_only" && -n "$tooltop_opt_high_cpu" && -n "$tooltip_opt_show_mem_procs" ]]; then
  123.         ps -eo pid,comm,%cpu,rss --no-headers --sort=-%cpu | \
  124.         awk -v mem_alert="$tooltip_opt_mem_alert" -v cpu_alert="$tooltip_opt_cpu_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" -v high_mem_flag="$tooltop_opt_high_mem" '
  125.            $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
  126.                mem_mb = int($4/1024);
  127.                icon_cpu = ($3 > cpu_alert) ? "🧠" : "";
  128.                icon_mem = (high_mem_flag && mem_mb > mem_alert) ? "💾" : "";
  129.                if(icon_mem != "" && icon_cpu != "") {
  130.                    printf "%s|%s (%s%d%% - %s%dMB)\n", $2, $2, icon_cpu, int($3), icon_mem, mem_mb
  131.                } else if(icon_mem != "") {
  132.                    printf "%s|%s (%d%% - %s%dMB)\n", $2, $2, int($3), icon_mem, mem_mb
  133.                } else if(icon_cpu != "") {
  134.                    printf "%s|%s (%s%d%% - %dMB)\n", $2, $2, icon_cpu, int($3), mem_mb
  135.                } else {
  136.                    printf "%s|%s (%d%% - %dMB)\n", $2, $2, int($3), mem_mb
  137.                }
  138.            }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
  139.         exit
  140.     fi
  141.     if [[ -n "$tooltip_opt_user_only" && -n "$tooltop_opt_high_cpu" && -z "$tooltip_opt_show_mem_procs" ]]; then
  142.         ps -eo pid,comm,%cpu --no-headers --sort=-%cpu | \
  143.         awk -v cpu_alert="$tooltip_opt_cpu_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" '
  144.            $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
  145.                icon = ($3 > cpu_alert) ? "🧠" : "";
  146.                if(icon != "") {
  147.                    printf "%s|%s (%s%d%%)\n", $2, $2, icon, int($3)
  148.                } else {
  149.                    printf "%s|%s (%d%%)\n", $2, $2, int($3)
  150.                }
  151.            }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
  152.         exit
  153.     fi
  154.     if [[ -n "$tooltip_opt_user_only" && -z "$tooltop_opt_high_cpu" && -n "$tooltip_opt_show_mem_procs" ]]; then
  155.         ps -eo pid,comm,%cpu,rss --no-headers --sort=-%cpu | \
  156.         awk -v mem_alert="$tooltip_opt_mem_alert" -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" -v high_mem_flag="$tooltop_opt_high_mem" '
  157.            $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
  158.                mem_mb = int($4/1024);
  159.                icon_mem = (high_mem_flag && mem_mb > mem_alert) ? "💾" : "";
  160.                if(icon_mem != "") {
  161.                    printf "%s|%s (%d%% - %s%dMB)\n", $2, $2, int($3), icon_mem, mem_mb
  162.                } else {
  163.                    printf "%s|%s (%d%% - %dMB)\n", $2, $2, int($3), mem_mb
  164.                }
  165.            }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
  166.         exit
  167.     fi
  168.     if [[ -n "$tooltip_opt_user_only" && -z "$tooltop_opt_high_cpu" && -z "$tooltip_opt_show_mem_procs" ]]; then
  169.         ps -eo pid,comm,%cpu --no-headers --sort=-%cpu | \
  170.         awk -v pspid="$$" -v hide_zero="$tooltip_opt_hide_no_cpu_use_procs" '
  171.            $2 != "ps" && $1 != pspid && !seen[$2]++ && (hide_zero ? int($3) > 0 : 1) {
  172.                printf "%s|%s (%d%%)\n", $2, $2, int($3)
  173.            }' | head -n "$tooltip_opt_n_procs" | while IFS="|" read -r comm rest; do print_with_icon "$comm" "$rest"; done
  174.         exit
  175.     fi
  176. fi
  177.  
  178. get_cpu_values() {
  179.     read -r _ user nice system idle iowait irq softirq steal guest guest_nice < /proc/stat
  180.     total=$((user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice))
  181.     echo "$idle $total"
  182. }
  183. read idle1 total1 < <(get_cpu_values)
  184. sleep 1
  185. read idle2 total2 < <(get_cpu_values)
  186. delta_idle=$((idle2 - idle1))
  187. delta_total=$((total2 - total1))
  188. cpu_usage=$((100 * (delta_total - delta_idle) / delta_total))
  189. if ((cpu_usage > 90)); then
  190.     echo -e " \e[91mī‹› ${cpu_usage}%\e[0m"
  191. else
  192.     echo "ī‹› ${cpu_usage}%"
  193. fi
  194.  
Advertisement
Add Comment
Please, Sign In to add comment