Created
September 24, 2020 08:25
-
-
Save derjohn/f87bc2bf19eed8cd204673b894b80223 to your computer and use it in GitHub Desktop.
It creates a systemd "slice" with a memory limit. Ideal if e.g. firefox or chromium eat up all your memory and block your whole desktop. The slice will be OOM killed by systemd / cgroups, if it runs of bounds.
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/bash | |
# It creates a systemd "slice" with a memory limit. Ideal if e.g. firefox or chromium eat up all your memory and block your whole desktop. | |
# The slice will be OOM killed by systemd / cgroups, if it runs of bounds. | |
# usage: ./create-memorylimited-slice /path/to/your/binary | |
SLICEPATH=${1:-'/usr/bin/firefox'} | |
SLICENAME=$(basename ${SLICEPATH}) | |
## Create system-wide scripts | |
cat <<EOF | sudo tee /usr/local/bin/${SLICENAME}-slice > /dev/null | |
#!/bin/sh | |
systemd-run --user --slice=${SLICENAME}.slice ${SLICEPATH} "\$@" | |
EOF | |
sudo chmod +x /usr/local/bin/${SLICENAME}-slice | |
cat <<EOF | sudo tee /usr/local/bin/${SLICENAME}-slice-status > /dev/null | |
systemctl --user status ${SLICENAME}.slice | |
EOF | |
sudo chmod +x /usr/local/bin/${SLICENAME}-slice-status | |
## Create user-based config | |
mkdir -p /home/aj/.config/systemd/user | |
cat <<EOF > ~/.config/systemd/user/${SLICENAME}.slice | |
[Slice] | |
MemoryMax=4G | |
EOF | |
cat <<EOF | |
Scripts installed, slice limit set to 4GB. It can be configured in this file ~/.config/systemd/user/${SLICENAME}.slice | |
Start the firefox slice with "${SLICENAME}-slice" | |
Check the Memory usage with "${SLICENAME}-slice-status" | |
EOF | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment