De-prioritizing a systemd-run job
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

initial commit

rektide 4f33ebfd

+121
+81
README.md
··· 1 + # gentle.sh 2 + 3 + Run commands with limited system resources to prevent builds from hammering system responsiveness. 4 + 5 + ## Table of Contents 6 + 7 + - [Background](#background) 8 + - [Install](#install) 9 + - [Usage](#usage) 10 + - [Resource Limits](#resource-limits) 11 + - [Examples](#examples) 12 + 13 + ## Background 14 + 15 + When running large builds or heavy computational workloads, system responsiveness can suffer. `gentle.sh` wraps any command in a systemd-run scope with aggressive resource limits to keep your system usable while the build runs. 16 + 17 + ## Install 18 + 19 + Either add to PATH: 20 + 21 + ```sh 22 + export PATH=$(pwd):$PATH 23 + ``` 24 + 25 + Or symlink 26 + 27 + ```sh 28 + sudo ln -s $(pwd)/gentle.sh /usr/local/bin 29 + ``` 30 + 31 + ## Usage 32 + 33 + ```bash 34 + gentle.sh <command> [args...] 35 + ``` 36 + 37 + ## Resource Limits 38 + 39 + The following resource controls are applied to all commands run through `gentle.sh`: 40 + 41 + ### CPU Limits 42 + 43 + - **CPU Affinity**: All CPUs except the first 2 (keeps cores 0-1 free for system) 44 + - **CPU Scheduling Policy**: `batch` - lower priority than normal processes 45 + - Available lower option: `idle` - only runs when nothing else needs CPU 46 + - **CPU Weight**: `1` - minimum weight (1-10000 range) 47 + - **CPU Quota Period**: `300ms` - period for quota measurement 48 + - **Nice Level**: `19` - lowest nice priority 49 + 50 + ### I/O Limits 51 + 52 + - **IO Weight**: `1` - minimum weight (1-10000 range) 53 + - **IO Scheduling Class**: `best-effort` with priority `7` - lowest best-effort priority 54 + - Available lower option: `idle` - lowest possible IO priority 55 + - **IO Device Latency**: `infinity` applied to all detected block devices - no strict latency enforcement on any drive 56 + 57 + ### Memory Limits 58 + 59 + - **MemoryMax**: `80%` - cap at 80% of system memory 60 + - **TasksMax**: `infinity` - unlimited processes 61 + 62 + ## Examples 63 + 64 + Build a large project without freezing your system: 65 + 66 + ```bash 67 + gentle.sh make -j8 68 + ``` 69 + 70 + Run a compilation: 71 + 72 + ```bash 73 + gentle.sh ./build.sh 74 + ``` 75 + 76 + ## Notes 77 + 78 + - The script uses `systemd-run --scope --user`, which requires systemd user sessions 79 + - Block devices are auto-detected using `lsblk` and IO latency targets are set for all drives 80 + - The process runs in the foreground (not detached as a service) 81 + - All resource limits are applied to the process and its children
+40
gentle.sh
··· 1 + #!/bin/bash 2 + 3 + # Run a command gently with limited CPU, IO, and nice values using systemd-run 4 + 5 + # Calculate CPU affinity (all CPUs except first 2) 6 + CPU_COUNT=$(nproc) 7 + CPU_AFFINITY="2-$(($CPU_COUNT - 1))" 8 + 9 + # Get all base block devices to set IO latency target on all drives 10 + BLOCK_DEVICES=$(lsblk -d -n -o NAME | grep -E "^(nvme|sd)") 11 + 12 + # Build systemd-run command 13 + # Note: IOSchedulingClass=idle is even lower priority than best-effort 14 + # Note: CPUSchedulingPolicy=idle is even lower priority than batch 15 + RUN_CMD=( 16 + systemd-run 17 + --scope 18 + --user 19 + --nice=19 20 + --io-weight=1 21 + --cpu-affinity="$CPU_AFFINITY" 22 + --property=CPUWeight=1 23 + --property=MemoryMax=80% 24 + --property=TasksMax=infinity 25 + --property=CPUQuotaPeriodSec=300ms 26 + --property=IOSchedulingClass=best-effort 27 + --property=IOSchedulingPriority=7 28 + --property=CPUSchedulingPolicy=batch 29 + ) 30 + 31 + # Add IODeviceLatencyTargetSec for each block device 32 + for device in $BLOCK_DEVICES; do 33 + RUN_CMD+=("--property=IODeviceLatencyTargetSec=/dev/$device 20s") 34 + done 35 + 36 + # Add the command to run 37 + RUN_CMD+=("$@") 38 + 39 + # Execute 40 + "${RUN_CMD[@]}"