Created
April 5, 2024 01:07
-
-
Save Deliaz/a2c10e426cab6370a34b9d85c504ce18 to your computer and use it in GitHub Desktop.
Bash script to update PM2-managed web services and verify online status post-rebuild. Stops the process, pulls code changes, runs npm install if needed, and restarts. Continuously checks HTTP status until the service is back online, reporting the time taken.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bash script to update PM2-managed web services and verify online status post-rebuild. Stops the process, pulls code changes, runs npm install if needed, and restarts. Continuously checks HTTP status until the service is back online, reporting the time taken.