Skip to content

Instantly share code, notes, and snippets.

@thomassuckow
Created November 18, 2014 23:24
Show Gist options
  • Save thomassuckow/db28b6c1224f614ff31a to your computer and use it in GitHub Desktop.
Save thomassuckow/db28b6c1224f614ff31a to your computer and use it in GitHub Desktop.
Script to limit the number of running processes to one more than the number of cores. Symlink this script to /usr/local/bin/ named as the binary you want to throttle.
#!/bin/bash
CMD=$(basename $0) # command used to call this script
CMD=$(type -a -p $CMD | sed -n 2p) #find the next highest priority
RUNNING=$(grep procs_running /proc/stat|cut -d " " -f 2)
BLOCKED=$(grep procs_blocked /proc/stat|cut -d " " -f 2)
PROCESSLIMIT=$(grep cpu /proc/stat | wc -l)
#Wait for running processes to die down
while [ $RUNNING -gt $PROCESSLIMIT ]; do
sleep 1
RUNNING=$(grep procs_running /proc/stat|cut -d " " -f 2)
BLOCKED=$(grep procs_blocked /proc/stat|cut -d " " -f 2)
PROCESSLIMIT=$(grep cpu /proc/stat | wc -l)
done
exec $CMD $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment