When running two tail commands in background, second tail is not working

119 views Asked by At

I am trying to run a process in remote machine and tail its stdout and stderr to my local machine.

But for some unknown reason, this command is tailing only stdout.

ssh <remote> 'tail /tmp/tmp.gLxtGhFRts/.stdout & tail /tmp/tmp.gLxtGhFRts/.stderr >&2 &'
---contents of stdout---

If I remove the trailing ampersand, both tails are working as expected.

ssh <remote> 'tail /tmp/tmp.gLxtGhFRts/.stdout & tail /tmp/tmp.gLxtGhFRts/.stderr >&2'
---contents of stdout---
---contents of stderr---

If I remove the trailing redirection, both tails are working as expected.

ssh <remote> 'tail /tmp/tmp.gLxtGhFRts/.stdout & tail /tmp/tmp.gLxtGhFRts/.stderr &'
---contents of stdout---
---contents of stderr---

Why is adding an ampersand(&) or redirection(>&2) at the end suppressing the second command? Can someone please explain this behaviour?

EDIT:

I tested the same thing with head and cat. The same behavior is seen with them also.

I tested the same thing with different files. The same behavior is seen with them also.

I sshed the remote and then tried all these combinations. All are working as expected. So I think the issue is with ssh.

EDIT:

I tried adding wait at the very end. And now it is working as expected.

But AFAIK, ssh waits until its stdin, stdout and stderr pipes close. So I'm not understanding why we need an explicit wait. Also, stdout is tailing fine without wait. So I still need an explanation.


EDIT: these all work as expected:

ssh remote 'cat /etc/hosts >&2'
ssh remote 'cat /etc/hosts &'
ssh remote 'cat /etc/hosts >&1 &'

but this produces no output:

ssh remote 'cat /etc/hosts >&2 &'

Why does that last one produce no output?

0

There are 0 answers