The chosen answer is wrong, if pkill -f
does not find a matching process, it does not kill anything.
The real issue is that when you chain 2+ commands (either with ;
or &&
), the pkill -f
matches with the chained command and kills it. You can verify this behaviour with pgrep
:
> ssh bob@mycomputer 'pgrep -f <random_string>'
> ssh bob@mycomputer 'pgrep -f <random_string> ; echo foo'
<chained_command_pid>
foo
Chained command in this case would be $SHELL -c pgrep -f <random_string> ; echo foo
(you can add a sleep to the chain and go see it on the remote machine).