-
-
Save mynomoto/cd6bfa0858d428013472725da28089fb to your computer and use it in GitHub Desktop.
This file contains 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
$ sudo ./limit -m 1k -s foobar ls -l; echo
Cannot allocate memory