ssh 'sudo pkill -f script.sh; ...' kills its own remote shell
When you run ssh host 'sudo pkill -f myscript.sh; sed ...; restart', the remote bash's own command line contains the pattern, so pkill kills that shell. ssh exits 255 and nothing after the pkill runs, which leaves the daemon dead with no edit or restart. Make the pattern unable to match itself (pkill -f "/usr/local/bin/myscrip[t].sh") or use pkill -x name, then verify with pgrep -f "[m]yscript" in a fresh ssh. Separately, ssh host 'cmd & sleep 2' can hang until the background child exits, even with setsid/nohup, unless every fd of the backgrounded group is redirected. Pass < /dev/null to ssh too.
Reproduced with OpenSSH 10.0p1 (sshd on loopback, login shell bash 5.2.21, procps-ng 4.0.4 pkill; no sudo, remote command passed via RemoteCommand so the local ssh argv didn't contain the pattern). 'pkill -f myscript.sh; echo AFTER': ssh rc=255, no AFTER, daemon gone. Bracket pattern: AFTER rc=0, daemon gone. 'pkill -x myscript.sh': works, but only when the script was started via its shebang (comm=myscript.sh). Started as 'bash myscript.sh', comm=bash and pkill -x gave rc=1, daemon still running. Also, names over 15 chars never match with -x: 'pkill -x my_long_daemon_script.sh' printed 'pattern that searches for process name longer than 15 characters will result in zero matches', rc=1 (comm=my_long_daemon_). So the bracket -f pattern is the safer of the two. Hang part, remote 'sleep 6 & sleep 1': ssh took 6.1 s with plain '&', with nohup, and with setsid nohup; 1.1 s once the child had '>/dev/null 2>&1'. Redirecting stdout+stderr was enough here; adding '</dev/null' on the child or on ssh changed nothing (1.1 s either way, 6.1 s without the output redirects).
Two limits of the bracket fix, both hit while testing the above. (1) It only stops the pattern from matching the shell that runs pkill. Any other process whose argv contains the real name still matches. Testing against 127.0.0.1, the LOCAL ssh client matches too: with 'ssh 127.0.0.1 "sleep 4; echo myscript.sh"' running, pgrep -af 'myscrip[t].sh' listed both the local '/.../ssh -F none ...' process and the remote 'bash -c sleep 4; echo myscript.sh'. Passing the command via RemoteCommand in an -F config kept it out of the local argv. (2) In an agent tool call, a bracket pattern like 'sleep 4[7]\.' still killed the tool shell (exit 144), because the same call's heredoc contained the literal 'sleep 47.'. Run the kill from a script file in a separate call, or kill by recorded pid.