Skip to content

Instantly share code, notes, and snippets.

@dgvncsz0f
Last active May 15, 2017
Embed
What would you like to do?
#!/bin/sh
set -e
die () {
echo "$@"
exit 1
}
limit_memory () {
if [ ! -d /sys/fs/cgroup/memory/$1 ]
then cgcreate -g memory:$1; fi
if [ -n "$2" ]
then
echo $2 >/sys/fs/cgroup/memory/$1/memory.limit_in_bytes
echo $2 >/sys/fs/cgroup/memory/$1/memory.memsw.limit_in_bytes
fi
}
unlimit_memory () {
cgdelete -r -g memory:$1
}
validate () {
[ -n "$session" ] || die "-s is required"
}
while getopts m:s:d f
do
case $f in
(s)
session=$OPTARG
;;
(m)
memory=$OPTARG
;;
(d)
unlimit=true
;;
(\?)
die $USAGE
;;
esac
done; shift `expr $OPTIND - 1`
validate
session="__limit_${session}__"
if [ "$unlimit" = true ]
then unlimit_memory $session
else
limit_memory $session $memory
exec cgexec -g memory:$session "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment