Skip to content

Instantly share code, notes, and snippets.

@caruccio
Last active April 20, 2024 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caruccio/f7f3822578f2929a1a8104e116f64f7a to your computer and use it in GitHub Desktop.
Save caruccio/f7f3822578f2929a1a8104e116f64f7a to your computer and use it in GitHub Desktop.
Pause processes during night
#!/bin/bash
#
# This script will stop a list of running proccesses (var PROCS), disable all monitors and CPUs (except cpu-0).
#
# Usage:
# pause [duration]
#
# With no parameters it will pause right alway.
# If [duration] is specified, it will detect for inactivity and pause, in an infinite loop. Use CTRL+C to exit.
# [duration] accepts the same as the command sleep.
#
set -eu
PROCS="slack telegram chrome brave firefox" ## Insert here your energy-consuming programs
LAST_CPU=$(lscpu -e=cpu|sed -ne '$s/ //gp')
if [ $# -gt 0 ]; then
PROCS="$@"
fi
function sig_proc()
{
local sig=${1:-CONT}
if [ "$sig" == STOP ]; then
echo Pausing processes: $PROCS
else
echo resuming processes: $PROCS
fi
printf "%s\n" $PROCS | xargs -l pkill "-$sig" || true
}
function set_cpu_state()
{
local state=${1:-1}
if [ "$state" -eq 0 ]; then
echo "Disabling CPUs [1..$LAST_CPU]"
else
echo "Enabling CPUs [1..$LAST_CPU]"
fi
echo $state | eval sudo tee "/sys/devices/system/cpu/cpu{1..$LAST_CPU}/online" >/dev/null
}
function monitor()
{
xset -display $DISPLAY dpms force ${1:-on}
}
function pause()
{
sig_proc STOP
set_cpu_state 0
monitor off
read -p 'Press [ENTER] to resume...'
monitor on
set_cpu_state 1
sig_proc CONT
}
if [ $# -eq 0 ]; then
pause
elif which xdotool &>/dev/null; then
TIMEOUT=$1
eval $(xdotool getmouselocation --shell --prefix=PREV_)
while true; do
echo Waiting $TIMEOUT
sleep $TIMEOUT
eval $(xdotool getmouselocation --shell --prefix CURR_)
#echo Current: $CURR_X, Last: $PREV_X
if [ $CURR_X -eq $PREV_X ]; then
pause
fi
PREV_X=$CURR_X
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment