Reputation: 95
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
Reputation: 23824
echo
to output the result of a sub shell.tee
to append to a file.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
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