6

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
sush
  • 95
  • 2
  • 8
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Dec 22 '17 at 06:51
  • 1
    It is about programming only. I got stuck in the automation scripting. Appreciate if anyone can help – sush Dec 22 '17 at 07:46

2 Answers2

6
  • 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 &'
ceving
  • 21,900
  • 13
  • 104
  • 178
  • Thanks. It is working i.e. updating the remote machine time after each 1 sec. But how to stop the background process. Is there any way? – sush Dec 22 '17 at 11:54
  • You have to store the [PID of the background process](https://stackoverflow.com/questions/1908610/how-to-get-pid-of-background-process) in order to kill it. – ceving Dec 24 '17 at 09:07
  • Thanks a lot ceving. I killed all the sleep process running in the background and it worked. – sush Jan 05 '18 at 05:02
4

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 \&'
Ben
  • 5,069
  • 4
  • 18
  • 26
  • 1
    It is not working. I am getting the Error opening terminal: unknown, even if i put -t option. – sush Dec 22 '17 at 11:55