Skip to content

Instantly share code, notes, and snippets.

@f33rx
Created February 28, 2019 18:42
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 f33rx/cecc84029298c5ac075c2b38da126e41 to your computer and use it in GitHub Desktop.
Save f33rx/cecc84029298c5ac075c2b38da126e41 to your computer and use it in GitHub Desktop.
Recursively renice
#!/bin/sh
PID_LIST=
platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
platform='linux'
elif [[ "$unamestr" == 'FreeBSD' ]]; then
platform='freebsd'
elif [[ "$unamestr" == 'Darwin' ]]; then
platform='freebsd'
fi
gnufindpids() {
[ "$#" -eq 1 -a -d "/proc/$1/task" ] || exit 1
for pid in /proc/$1/task/* ; do
pid="$(basename "$pid")"
PID_LIST="$PID_LIST$pid "
for cpid in $(cat /proc/$1/task/$pid/children) ; do
findpids $cpid
done
done
}
bsdfindpids() {
pgrep -P $1
}
macfindpids() {
if [ `which pstree` != 0 ]; then
brew install pstree
else
echo "please install homebrew and or pstree"
fi
pstree -p $1 | awk -F' ' '{if(NR>1)print $2}'
}
if [[ $platform == 'Linux' ]]; then
gnufindpids $1
echo $PID_LIST
elif [[ $platform == 'FreeBSD' ]]; then
bsdfindpids $1
echo $PID_LIST
elif [[ $platform == 'Darwin' ]]; then
macfindpids $1
echo $PID_LIST
fi
# If process PID 1234 is the one you want to recursively nice, now you can do:
# renice -n 15 -p $(/path/to/findchildren.sh 1234)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment