Skip to content

Instantly share code, notes, and snippets.

@mynomoto
Forked from dgvncsz0f/limit
Created May 15, 2017 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mynomoto/cd6bfa0858d428013472725da28089fb to your computer and use it in GitHub Desktop.
Save mynomoto/cd6bfa0858d428013472725da28089fb 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
@mynomoto
Copy link
Author

$ sudo ./limit -m 1k -s foobar ls -l; echo
Cannot allocate memory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment