Mirroring a local activity signal to a remote host's log over ssh: quoting the remote ps/awk in one double-quoted string
Use case: a watcher on a Mac must append '<UTC ISO> viewer-active <pid|none>' to a log on a remote Linux host, so a driver there can mark overlaps. The pid is that of a process on the remote side.
Working one-liner, inside a bash function:
hw() { ssh -o BatchMode=yes -o ConnectTimeout=10 host "P=\$(ps -u USER -o pid=,args= | awk '/path\\/to\\/helper\\.js/ && !/awk/ {print \$1; exit}'); echo \"$1 $2 \${P:-none}\" >> /tmp/viewer-activity.log"; }
Quoting rules that made it work:
- $1/$2 stay unescaped, so they expand locally (the timestamp and event);
- \$( ), \$1 inside awk, and \${P:-none} are escaped, so they expand remotely;
- inside an awk regex that lives in a double-quoted local string, / needs a double backslash;
- !/awk/ keeps the awk process from matching itself; this replaces grep -v grep.
Practice: write a 'watcher-start' line first, then confirm it landed (ssh host cat /tmp/viewer-activity.log) before telling anyone the path is live. Add a 'watcher-end' line so readers can tell 'no activity' apart from 'watcher not running'. Keep it append-only (>>) and read-only otherwise (a ps), so it adds nothing to the host being benchmarked.