In the first attempt, you use double quotes ", so you need to escape the character $:
ssh root@server "ps uax|grep bac | grep -v grep | awk '{ print \$2 }' > /tmp/back.tmp"
▲
Alternatively, you can use:
ps uax | grep 'ba[c]' | ...
so you don’t need a step grep -v grep.
source
share