#!/bin/bash #this script will reset gnome-shell when its memory usage is above threshold and machine is idle for 5 minutes MAXMEM_1=800000 MAXMEM_2=1300000 IDLE=300000 export DISPLAY=:0 function getmem { mem=`ps -ef -orss=,args= | sort -b -k1,1n | pr -TW128 | grep gnome-shell | grep USER=$USER | cut -d" " -f1 | tail -n 1` if [ -z $mem ]; then mem=`ps -ef -orss=,args= | sort -b -k1,1n | pr -TW128 | grep "gnome-shell --replace" | cut -d" " -f1 | tr -d '\n' | tail -n 1` fi echo $mem } while [ 1 ]; do if [ -e /tmp/resetgnome ]; then rm -f /tmp/resetgnome dbus-send --type=method_call --print-reply --dest=org.gnome.Shell /org/gnome/Shell org.gnome.Shell.Eval string:'global.reexec_self()' fi sleep 1 done & while [ 1 ]; do mem=$(getmem) echo MEM=$mem if [ $mem -gt $MAXMEM_1 ]; then echo "time to reset gnome-shell" while [ 1 ]; do mem=$(getmem) idle=`export DISPLAY=:1; xprintidle` echo "IDLE: $idle MEM: $mem" if [ $idle -gt $IDLE ] || [ $mem -gt $MAXMEM_2 ]; then touch /tmp/resetgnome break fi sleep 1 done fi sleep 3 done