Skip to content

Instantly share code, notes, and snippets.

@ananace
Last active October 12, 2023 14:13
Show Gist options
  • Save ananace/044c82425f4f1ac561506be9ad15d486 to your computer and use it in GitHub Desktop.
Save ananace/044c82425f4f1ac561506be9ad15d486 to your computer and use it in GitHub Desktop.
Small script to launch a process in a slice/cgroup
#!/bin/bash
set -euo pipefail
# Drop in /usr/local/bin/run-in-slice
#
# Requires system to be running systemd and cgroupsv2
SLICE="$1"
shift
[[ "$SLICE" = *".slice" ]] || SLICE="${SLICE}.slice"
SLICE_DIR="/sys/fs/cgroup/user.slice/user-$UID.slice/user@$UID.service/$SLICE"
mkdir -p "$SLICE_DIR"
echo "Moving to $SLICE_DIR"
echo $$ > "$SLICE_DIR/cgroup.procs"
exec $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment