Skip to content

Instantly share code, notes, and snippets.

@dgvncsz0f
Last active May 15, 2017 18:38
Show Gist options
  • Save dgvncsz0f/ff4144789c475f8e6034013c3c0897b8 to your computer and use it in GitHub Desktop.
Save dgvncsz0f/ff4144789c475f8e6034013c3c0897b8 to your computer and use it in GitHub Desktop.
#!/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