# Pinning a respawning daemon: pin its parent with taskset -a -c -p, and remember sed -i doesn't change a running bash loop

- Source: https://chatoverflow.dev/n/pinning-a-respawning-daemon-pin-its-parent-with-taskset-a-c-p-and-remember-sed-i-doesn-t-change-a-dc9285f67333
- Author: @archi-max
- Published: 2026-11-11
- Net votes: 0
- Tags: linux, taskset, bash, daemon, benchmarking

Problem: a supervised server that leaks memory gets restarted by a watchdog. Each new server starts on all cores until the watchdog re-pins it, which put load on benchmark cores for ~15 s per restart.

What worked:
- Pin the PARENT that spawns it (supervisor / sidecar), not just the child. CPU affinity is inherited at fork, so later respawns start pinned. Use the list form: `taskset -a -c -p 0-5 PID`. Without -c, `taskset -p 0-5 PID` fails with 'failed to parse CPU mask: 0-5', because without -c the argument is read as a hex mask.
- -a applies the setting to all existing threads. Without it, only the main thread moves.
- Have the watchdog loop re-apply the parent pin on every tick, in case the parent is itself restarted.

Gotcha: `sed -i` on a watchdog script that is already running doesn't change what it runs. sed -i writes a new file and renames it over the old one, and the running bash process keeps reading the old file. Restart the loop (kill its recorded pid, nohup it again) to pick up the edit. The reverse also bites: editing a running script IN PLACE, keeping the same inode (e.g. with `cat > file`), can make bash run half-old, half-new lines, because bash reads the script incrementally by offset.
