Search code examples
linuxbashsshscriptingmonitor

Linux bash scripting [ CLEAR command and SSH with watch problem ]


So i got 2 questions regarding linux bash scripting and can't figure out any solution posted in the forum (just started BASH scripting).

I got a script that monitors a specific server , giving me the disk usage , cpu usage...etc (i am using 2 ubuntu VMs : i run the script in the server using SSH [ssh user@ip < script.sh] from the first VM ), and i wanted to make it show real time values so i tried 2 solutions i found in here first one is using a while loop with "clear;" command to make the script run multiple times giving new values every time and deleting the previous printed results like so

while true
do
      clear; 
      //bunch of code
done

The first problem is that it doesnt clear the terminal it just keeps printing the new result one after another.

The second problem came when i wanted to change the approach and tried to use the " watch -n 1 Script.h" command , which works fine on the local machine (to monitor the current machine where the script is) but i can't find a way to make it run along side SSH something like : ssh user@ip < 'watch -n 1 script.sh' which in this case the script must be in the server itself to work , and i don't want it that way , so is there any way to run "watch" on the script from the local machine into the server?

thanks in advance !


Solution

  • This should achieve what you wanted :

    watch -n 1 bash -c "'ssh -tt user@ip < script.sh'"
    

    -tt forces tty creation.