Skip to content

Instantly share code, notes, and snippets.

@thefotios
Created May 9, 2012 17:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thefotios/2646996 to your computer and use it in GitHub Desktop.
Save thefotios/2646996 to your computer and use it in GitHub Desktop.
Renice a process and all of its children recursively
#!/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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment