Skip to content

Instantly share code, notes, and snippets.

Created July 13, 2015 21:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Bash: Ensure processes run on allowed CPUs
# function to expand the range of allowed CPUs
range_expand () (
set -- $1
for element; do
if [[ $element =~ ^(-?[0-9]+)-(-?[0-9]+)$ ]]; then
set -- "$@" $(eval echo "{${BASH_REMATCH[1]}..${BASH_REMATCH[2]}}")
set -- "$@" $element
shift $n
echo "$@"
# Loop through all processes
for i in $(ps -A -o pid=); do
allowed=$(range_expand $(grep Cpus_allowed_list /proc/${i}/status | awk '{ print $2 }'))
running=$(set -- $(</proc/${i}/stat); echo ${39})
if [[ "${allowed/$running}" = "${allowed}" ]] && [[ "${allowed}" ]]; then
printf "Process %d running on CPU %d but only allowed on %s\n" "$i" "$running" "$allowed"
done 2>/dev/null # otherwise we get errors for some short lived processes like the grep invocation used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment