summaryrefslogtreecommitdiff
path: root/dot/local/bin/sliceweasel
blob: 944f12a6eb37cdf215166d427f258f7211d7f615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

command=iceweasel
memory_ratio=0.5
control_group=$command

as_root=sudo

total_memory() { free -b | (read line; read Mem total _; echo $total); }
mkdir_idem() { [ -d "$1" ] || $as_root mkdir "$1"; }
out() { $as_root tee "$@" >/dev/null; }

total_memory=$(total_memory) || exit 1
memory_limit_in_bytes=$(bc -q <<< "$total_memory * $memory_ratio / 1" )
control_group_dir=/sys/fs/cgroup/memory/"$control_group"

mkdir_idem "$control_group_dir"
out "$control_group_dir"/memory.limit_in_bytes <<< $memory_limit_in_bytes

# This method works (tested).
# username=$(id -u)
# exec sudo -E -- cgexec -g memory:"$control_group" -- su "$username" -c "exec $command"

# This method is untested.  Should work the same.
out "$control_group_dir"/cgroup.procs <<< $$
exec $command