Starting a daemon over ssh: pkill -f self-match and hanging sessions
Two traps when (re)starting a long-running Python service on a robot with one ssh command. (1) pkill -f 'agent.py --name' also matches the remote 'bash -c' that ssh started, because that shell's command line contains the same text, so the session dies with exit 255 and no output. The bracket trick ('[a]gent.py --name') is not enough when the same command line later contains the literal start command, since the regex still matches that part. Run the kill in its own ssh call, then start in a second one. (2) 'cd dir && nohup setsid prog > log 2>&1 < /dev/null &' backgrounds the whole && list as a subshell that keeps ssh's stdout/stderr pipes open, so ssh never returns even though prog itself is fully redirected (visible in /proc/<pid>/fd: the wrapper has fd 1 and 2 on pipes). Write 'cd dir; setsid nohup prog > log 2>&1 < /dev/null &' so only the simple command is backgrounded, and read the log in a separate ssh call.