Skip to main content

Automated jobs and updating of a running website

Automating git and reloading websites using cron

We have in the previous section seen how we can "hide" the processes running our website. However, with an infrastructure using scripts to run our websites, it would be nice to avoid the following:

  1. Logging into the VM instance to perform a git pull every time we update the repository and
  2. performing a manual reload of the script running our docusaurus website every time we have commited changes to our Git repository.

To automate the steps above, we can use cron to make our life easier.

In this step, we will first make a shell script called gitpuller.sh, which will check if we have new content on our Git repository, and if that is the case, perform a git pull action and reload the script running our website on the VM.

first off, we will create a new folder (which will be in the same directory as the my-website folder):

mkdir ~/shell-scripts

then we will make our script:

cd shellscripts
nano gitpuller.sh

in the nano editor, add the following lines:

#!/bin/bash

# Set your tmux session name
SESSION_NAME="docusaurus"


# Check for updates
git fetch origin

# Compare local and remote branches
if ! git diff --quiet HEAD origin/$(git rev-parse --abbrev-ref HEAD); then
echo "Changes detected, pulling updates..."
git pull origin $(git rev-parse --abbrev-ref HEAD)

# Restart tmux session
if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
echo "Restarting tmux session: $SESSION_NAME"
tmux kill-session -t "$SESSION_NAME"
fi
tmux new-session -d -s "$SESSION_NAME"
tmux send-keys -t "$SESSION_NAME" "npx docusaurus start" C-m

echo "tmux session $SESSION_NAME restarted, docusaurus rebooted"
else
echo "No changes detected."
fi

In brief, this script does the following:

  1. Checks if there has been changes commited to the Git repository associated with the current working directory.
  2. If there is changes, perform a ´git pull´request to update the files
  3. Kill any previous tmux sessions running the docusaurus site (if any)
  4. Start a new tmux session which runs the updated docusaurus site

NOTE: The line tmux new-session -d starts the session, while the line tmux send-keys adds lines of commands which the tmux session will perform. Each line here is ended by C-m, which is equivalent to carriage return/pressing the 'Enter' key.

Now, we will automate the running of the above script. First, we have to enable the script to run - this is achieved by running

chmod +x ./gitpuller.sh

We can test the script by going to the relevant docusaurusrepository and run

../shell-scripts/gitpuller.sh

Try modifying some of the contents in the repository which hold the docusaurus files and rerun the script and see what happens.

Now, we will schedule how often this script will run. If cron is not installed on your VM, you can do this by running

sudo apt install cron -y

Then, run

sudo systemctl enable cron
sudo systemctl start cron

to make sure that it is running in the background and keeps running even if the server is rebooted. To verify that cron is working, you can use

sudo systemctl status cron

Now, after making sure that cron has been set up, run

crontab -e

If this is the first time you are using cron, you will be prompted to select an editor to configure your crontab. To choose nano, type 1, press Enter and move on.

Now, you should be met with the following window:

alt text

In this textfile we can configure how often we want to run certain tasks. The five stars given before the command denote:

# * * * * * <command to execute>
# | | | | |
# | | | | day of the week (0–6) (Sunday to Saturday;
# | | | month (1–12) 7 is also Sunday on some systems)
# | | day of the month (1–31)
# | hour (0–23)
# minute (0–59)

ie. if we want to run a task every day at 05:00, for example a backup of all user accounts, we can add the line:

0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

For our usecase, we will add this line to the bottom of this textfile:

*/1 * * * * -su -s /bin/sh user-name -c 'cd ~/docusaurus-mwe && ~/shell-scripts/gitpuller.sh'

which will, at every minute, switch the user user-name, change the directory to the directory where the docusaurus files reside and run the gitpuller.sh script.

This way, every time we have made a commit/merge to the main branch of the corresponding Git repository, we are sure that our VM will fetch the updated version and rerun the script

Note: Different crontabs exist for different users on a VM. The above line will only work for the root user of the VM.