#!/bin/bash # Configuration variables PROCESS_ID=1 # PM2 process ID DOMAIN_URL=https://example.com # Domain to check CHECK_INTERVAL=10 # Interval for HTTP check in seconds SERVICE_DIRECTORY=path-to-folder/ # Directory of the web service # Function to check HTTP status code check_http_status() { start_time=$(date +%s) while true; do http_code=$(curl -o /dev/null -s -w "%{http_code}\n" -L $DOMAIN_URL) current_time=$(date +%s) elapsed_time=$((current_time - start_time)) if [ "$http_code" -eq 200 ]; then echo "Received 200 OK after $elapsed_time seconds." return else echo "Waiting... Time elapsed: $elapsed_time seconds." sleep $CHECK_INTERVAL fi done } # Main script operations # Listing PM2 processes pm2 ls # Stopping the process pm2 stop $PROCESS_ID # Changing directory to the service directory cd $SERVICE_DIRECTORY # Pulling the latest changes from the git repository git pull # Checking if package.json was modified if git diff HEAD@{1} HEAD --name-only | grep -q "package.json" then echo "package.json changed, running npm i..." npm i else echo "package.json not changed" fi # Starting the process pm2 start $PROCESS_ID # Checking the HTTP status after starting the application echo "Checking the HTTP status of $DOMAIN_URL..." check_http_status