Skip to content

Instantly share code, notes, and snippets.

@JohannesBuchner
Created May 12, 2020 08:56
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 JohannesBuchner/85d8c8f1620694df4b09c529437a10af to your computer and use it in GitHub Desktop.
Save JohannesBuchner/85d8c8f1620694df4b09c529437a10af to your computer and use it in GitHub Desktop.
Stop/resume processes when the user is active/inactive in Linux/Xorg
#!/bin/bash
# how to identify the processes to STOP/CONT
# this is any part of process command line
PROCESS="myscript.py"
# run this in one terminal:
# it tries to resume the processes every so often
while sleep 10m; do
pgrep -f "${PROCESS}"|xargs -rt kill -CONT
done
# run this in another terminal:
# it stops the processes whenever there is any X event
# all the other stuff is for reducing the number of calls
xinput test-xi2 --root |
grep --line-buffered EVENT |
while read i; do date +%s; done | stdbuf -oL uniq |
while read j; do pgrep -f "${PROCESS}" |xargs -rt kill -STOP; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment