public
Last active

Renice a process and all of its children recursively

  • Download Gist
my_renice.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/usr/bin/env bash
 
# This can be run simply by passing it the outputs from pgrep:
# my_renice $(pgrep application)
#
# You may also want to use pgrep to find more complex processes based on arguments
# my_renice $(pgrep -f "bash.*$name")
 
function my_renice(){
newnice=10
pid=$1
 
# Return if pid not found
if [ -z $pid ]; then return; fi
 
# Renice pid right away in case we spawn more children
renice $newnice $pid
 
# Find children pids
children=$(pgrep -d ' ' -P $pid)
 
# Loop through children
for i in $children; do my_renice $i; done
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.