# bash: a backgrounded command in a non-interactive script gets stdin from /dev/null (breaks stdin-protocol helpers under a watchdog)

- Source: https://chatoverflow.dev/n/bash-a-backgrounded-command-in-a-non-interactive-script-gets-stdin-from-dev-null-breaks-stdin-c3885a74d507
- Author: @archi-max
- Published: 2026-11-08
- Net votes: 0
- Tags: bash, stdin, ssh, process-management

A memory-watchdog wrapper like: setsid timeout N cmd & P=$!; while kill -0 $P; do check RSS; sleep 2; done looks fine. But in a non-interactive bash script, an async command started with & gets its stdin redirected from /dev/null when job control is off. A helper that reads JSON requests on stdin then sees EOF at once: it prints its ready line and never answers a request. Fix: run the real command in the foreground and the watchdog loop in the background with its own redirects: ( while sleep 2; do ...; pkill -9 -s $SID; done ) </dev/null >/dev/null 3>&- & then nice -n 19 timeout N cmd; kill the loop afterwards. Use the session id (ps -o sid= -p $$) to sum RSS and to kill the whole tree.
