Restarting the service automatically when specific text doesn’t exist on a webpage is a good trick to keep the application functional while you fix the real issue.

While working on Geekflare Tools, we noticed that front-end is losing connection with CMS for some reason; hence webpage shows default content which gives bad user experience and confuses search engines.

I am using headless CMS called Directus to manage content.

<img alt="geekflare website audit page broken" data-src="https://kirelos.com/wp-content/uploads/2024/09/echo/geekflare-website-audit-page-broken.png" decoding="async" height="814" src="data:image/svg xml,” width=”900″>

Finding the real root cause is time-consuming and while waiting for real issue fixes, here is the workaround I followed and you can do too.

  • Requirement – auto-restart services when expected content doesn’t exist on a webpage.
  • What to restart – CMS and front-end service. (technically, you can restart anything depending on your needs)

I used Python with BeautifulSoup module to do the following.

  • Scrape webpage (ex – https://geekflare.com/tools/website-audit)
  • Look for expected content (ex – Geekflare Website Audit)
  • If expected content not found then restart CMS and front-end service
  • Put script in crontab to run every 10 minutes

It is advisable to do this on the server where the services run, so you avoid the complexity of running scripts remotely.

  • Login to server (I am using Ubuntu)
  • Create a .py file (autostart.py) in your desired folder and use below script
import requests
from bs4 import BeautifulSoup
import os
import time

# URL to scrape, feel free to change this per your needs.
url = "https://geekflare.com/tools/website-audit"

# Send a request to fetch the webpage content
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
    soup = BeautifulSoup(response.content, 'html.parser')

    # Check if the expected text is present on the page, change the text as you need.
    if "Geekflare Website Audit" not in soup.get_text():
        print("Expected Text not found, running custom script...")

        # Directus CMS Docker container
        os.system('/usr/bin/docker restart directus')

        # Giving sleep for 30 seconds to allow the restart process to complete
        time.sleep(30)

        # Restart frontend using PM2
        os.system('/usr/bin/pm2 restart tools_fe')

        print("Autostart script executed successfully.")
    else:
        print("Expected Text found, no action needed.")
else:
    print(f"Failed to fetch the page. Status code: {response.status_code}")

I’ve added the comment inside the script so you can adjust the necessary details per your needs.

  • Save the file, let’s call it autostart.py
  • Add the Python script in crontab
  • Open crontab
crontab -e
  • Add below and save the crontab
*/10 * * * * /usr/bin/python3 /path/to/autostart.py >> /path/to/autostart.log 2>&1

Above tiny script will run through system cron every 10 minutes to scrape a webpage and restart services when expected content doesn’t exist on a webpage. I would advise testing the script to ensure it works for you.

Was this helpful?

Thanks for your feedback.