Last active
May 15, 2017 18:38
-
-
Save dgvncsz0f/ff4144789c475f8e6034013c3c0897b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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