Create a watch dog script for Local Git Repository to Pull commits !

 How to create a watch dog scrip for a git folder in your local computer?

Need: We clone a public repository to our local computer and executing the code from there. When doing  commit to the pull request done in the upstream master, in each and every time we need to pull the same manually to our local repository. 

To solve the issue, we can create a script and configure the same to the start up options will update the local repository with respect to  the parent repository changes.


Clone a repository to a local folder using the following commands


In Linux Terminal, run the following code:

git clone https://<link_to_the_GitHub_repository>

Now what i doing is,  am renaming the cloned repository to 'git_folder'

Change the directory to the 'git_folder' using cd command 


Run in Linux terminal:


nano script_run.sh                   #Save this code as script_run.sh (Can customise the name)


When opened, write the following code with your parameters:


#!/bin/sh

cd <path_to_the_git_folder>   #Change the directory to the 'git_folder' using cd command

git remote add upstream <https://<link_to_the_GitHub_repository>

while :                                      #create a while loop to the run the code continuously

do

    cd <Path_to_the_local_repository>

    git pull upstream master

    sleep 30                                 #30Sec code delay for the next fetch

done        


Save the code and exit:


Now make the script executable, Run:

chmod 755 <script_run.sh>


Now open the super user console by Run:

sudo su


Enter the sudo password:


Run the following command:

sudo crontab -e


When in first run, it will as the default text editor to be opened. Select the required . Default is nano

After opening the file move to the bottom of the file and create an entry like the following code:

@reboot <path_to_the_script_file>


Save and exit the editor.

Now reboot the pc by running the command:

sudo reboot


When after reboot, your code will run behind the desktop as a watch dog for the local repository. It will always check for the parent repo changes i the intervals (delay) given in the code.


You can run almost every script behind link this method.


Happy Coding !



Comments

Popular posts from this blog

Clonebot Technical Documentation

Run your apps in the system background

Self-Regulating Battery Charger Circuit