Skip to content

Instantly share code, notes, and snippets.

@Wizek
Created November 28, 2016 06:35
Show Gist options
  • Save Wizek/4a9084bbe2d9a20fa94d8fc65e75f53e to your computer and use it in GitHub Desktop.
Save Wizek/4a9084bbe2d9a20fa94d8fc65e75f53e to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
set -e
term="$1"
echo $term
loop=1
asleep=.95
awake="`echo "$loop - $asleep" | bc`"
echo "asleep: $asleep, awake: $awake"
trap cleanup INT
function cleanup() {
function f () { echo "\tExiting: "$1}
if [ -n "$lastKnownPid" ]; then
f "continuing \$lastKnownPid ($lastKnownPid)"
kill -SIGCONT $lastKnownPid
else
f "no \$lastKnownPid"
fi
}
function log () {
if [[ $1 -lt 2 ]]; then
echo $2
fi
}
while true; do
if [ -n "$lastKnownPid" ]; then
kill -SIGCONT $lastKnownPid
fi
sleep $awake
{
pid=$(xdotool getwindowfocus getwindowpid)
cmdline="$(cat "/proc/$pid/cmdline")"
pCwd="$(readlink "/proc/$pid/cwd")"
# TODO use $cmdline as matchbase instead
# TODO or use both
# matchbase=$cmdline
matchbase="$pCwd $cmdline"
if [ -e "/proc/$lastKnownPid/" ]; then
# noop
else
lastKnownPid=''
echo 'App runs no more, resetting $lastKnownPid'
fi
if (echo "$matchbase" | grep -q $term); then
log 2 "match ($pid): in $matchbase"
lastKnownPid=$pid
# kill -SIGCONT $pid
else
log 2 "no match ($lastKnownPid): $matchbase"
if [ -n "$lastKnownPid" ]; then
kill -SIGSTOP $lastKnownPid
fi
fi
} || {
echo 'An error happened. Continuing'
}
sleep $asleep
done
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment