Skip to content

Instantly share code, notes, and snippets.

@JayGoldberg
Forked from simonklee/cgroup-chrome
Last active April 11, 2017 15:21
Show Gist options
  • Save JayGoldberg/954f683ab1795e6b07efa65212db7ac0 to your computer and use it in GitHub Desktop.
Save JayGoldberg/954f683ab1795e6b07efa65212db7ac0 to your computer and use it in GitHub Desktop.
#!/bin/bash
## @author Jay Goldberg
## @email jaymgoldberg@gmail.com
## @license Apache 2.0
## @description Run web browser in a cgroup
## @usage cgroup-chrome
#=======================================================================
MB=${$1:-1024}
browser=${$2:-"google-chrome-beta"}
url=${$3:-"http://www.kogama.com/games/play/68818/?da=1&webgl=1"}
# check that browser exists? why not use stat or ls
if type $browser &>/dev/null; then
browser="$(which ${browser})"
else
exit 1;
fi
re='^[0-9]+$'
if ! [[ $MB =~ $re ]] ; then
echo "error: Not a number" >&2; exit 1
fi
BYTES=$(echo $((${MB}*1024*1024)) | cut -d. -f1)
SHARES=1024
SWAPPINESS=0
echo "Memory available $MB MB"
echo "`${browser} --version`"
# make sure a "chrome" cgroup exists in the user tree
[ -e /sys/fs/cgroup/memory/user.slice/user-${UID}.slice/chrome ] || \
sudo cgcreate -a ${USER} -g memory,cpu:user.slice/user-${UID}.slice/chrome
# fix privileges
sudo chown -R ${USER} /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome
sudo chmod -R 777 /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome
# set the limits
echo ${BYTES} > /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome/memory.limit_in_bytes
echo ${SHARES} > /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome/cpu.shares
echo ${SWAPPINESS} > /sys/fs/cgroup/user.slice/user-${UID}.slice/chrome/memory.swappiness
# reparent ourselves, just because we're nice and doing things the right ways
# and stuff (doesn't really do much)
cgclassify -g memory,cpu:user.slice/user-${UID}.slice/chrome $$
# start browser
if [[ $browser == *"firefox"* ]]; then
cgexec -g memory,cpu:user.slice/user-${UID}.slice/chrome "${browser}" -private-window $url
else
cgexec -g memory,cpu:user.slice/user-${UID}.slice/chrome "${browser}" --incognito $url
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment