Skip to content

Commit ba324f7

Browse files
NullVoxPopuliclaude
andcommitted
Use sudo nice for high priority without requiring root shell
Instead of requiring the user to run the whole command as root (which loses PATH and can't find node/pnpm), use 'sudo nice -n -20 node ...' so only the nice call is elevated. Activates automatically when passwordless sudo is available. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 75fea38 commit ba324f7

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

scripts/bench-compare.mjs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,19 @@ try {
119119
console.error('📌 CPU pinning enabled (taskset -c 0)');
120120
}
121121

122-
if (HAS_NICE && process.getuid?.() === 0) {
122+
const IS_ROOT = process.getuid?.() === 0;
123+
const HAS_SUDO = IS_LINUX && spawnSync('sudo', ['-n', 'true'], { stdio: 'pipe' }).status === 0;
124+
125+
if (HAS_NICE && IS_ROOT) {
123126
fullArgs = ['-n', '-20', cmd, ...fullArgs];
124127
cmd = 'nice';
125128
console.error('⚡ High priority enabled (nice -n -20)');
129+
} else if (HAS_NICE && HAS_SUDO) {
130+
fullArgs = ['nice', '-n', '-20', cmd, ...fullArgs];
131+
cmd = 'sudo';
132+
console.error('⚡ High priority enabled (sudo nice -n -20)');
126133
} else if (HAS_NICE) {
127-
console.error('💡 Run with sudo for high scheduling priority (nice -n -20)');
134+
console.error('💡 High priority unavailable (needs passwordless sudo)');
128135
}
129136

130137
console.error('');

scripts/run-bench.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ if command -v taskset &>/dev/null; then
1717
echo "📌 CPU pinning enabled (taskset -c 0)" >&2
1818
fi
1919

20-
# High scheduling priority (best-effort, needs root for negative values)
20+
# High scheduling priority (best-effort, negative nice needs root)
2121
if command -v nice &>/dev/null; then
2222
if [ "$(id -u)" = "0" ]; then
2323
CMD=(nice -n -20 "${CMD[@]}")
2424
echo "⚡ High priority enabled (nice -n -20)" >&2
25+
elif command -v sudo &>/dev/null && sudo -n true 2>/dev/null; then
26+
CMD=(sudo nice -n -20 "${CMD[@]}")
27+
echo "⚡ High priority enabled (sudo nice -n -20)" >&2
2528
else
26-
echo "💡 Run with sudo for high scheduling priority (nice -n -20)" >&2
29+
echo "💡 High priority unavailable (needs passwordless sudo)" >&2
2730
fi
2831
fi
2932

0 commit comments

Comments
 (0)