sush
sush

Reputation: 95

Using watch with ssh

I have the below commands which is running properly on the local machine.

watch -t -n1 "echo `date '+%Y-%m-%d %H:%M:%S'` | tee -a Time.txt" &>/dev/null &

But when I run it from the remote machine I won't create the expected output i.e. Time.txt is not created and will not be running as a background process.

ssh ipaddress watch -t -n1 "echo `date '+%Y-%m-%d %H:%M:%S'` | tee -a Time.txt" &>/dev/null &

Please help me on this. I have tried multiple options like putting ', " for watch command but it did not helped me out.

Upvotes: 6

Views: 4562

Answers (2)

ceving
ceving

Reputation: 23824

  • No need to use echo to output the result of a sub shell.
  • No need to use tee to append to a file.
  • No need to use watch to sleep for one second.

Try this instead.

ssh -t ipaddress 'while sleep 1; do date +%Y-%m-%d\ %H:%M:%S >> Time.txt; done &'

Upvotes: 6

Ben
Ben

Reputation: 5129

You had incorrect shell syntax: explainshell

and throws this error when running the command:

Error opening terminal: unknown.

You should use the option -t

-t      Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a
        remote machine, which can be very useful, e.g. when implementing menu services.  Multiple -t
        options force tty allocation, even if ssh has no local tty.

And quote the whole command and pass it to ssh, and escaped characters when neccessary. attached explainshell

ssh -t ipaddress 'watch -t -n1 "echo `date +%Y-%m-%d\ %H:%M:%S` | tee -a Time.txt" \&\>/dev/null \&'

Upvotes: 5

Related Questions