I am comparing a file on my linux machine with another file on a remote machine (both AWS Instances) using the following command:
diff -s main <(ssh -i /home/ubuntu/sai_key.pem ubuntu@w.x.y.z 'cat /home/ubuntu/c1')
I want to write a shell script to do nothing when both files are same and update the file in the remote machine when the file in the linux machine (host) changes.
I want the shell script to check the remote file every 30 seconds.
I am running the shell script on my host machine only and not the remote one.
Could you please help me with that?
First of all, I would recommend using
cmp
rather thandiff
(I believe it is more efficient), but this solution should work either way.All you need to do is write a bash script with an if statement in it. If the
cmp
ordiff
command returns nothing, you don't need to take any action. In the other case, you just need toscp
your currentmain
file over to the remote host.If you decide to use
cmp
, that if statement just needs to look like:If you really are dead set on using
diff
, comment below and I can write something up really quick that does the equivalent.Checking the remote file (running this bash script) every 30 seconds may be slightly overkill, but it's really up to you. To achieve a periodic check (this only works for time intervals above 1 minute), you could use a cron scheduler. I recommend using Crontab Guru to create cron schedules and understand how they work. For your purpose, you would just need to add one line to your crontab (in your terminal run
crontab -e
to edit your crontab) as follows:Make sure you
chmod
the script with the right permissions as well!There is no need for bash, diff, cmd, cron, ... Python can do everything with a little help from ssh: