Files
raspi-iot-ryhmatyo/watch.sh

62 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# Configuration
BRANCH="main" # Set your branch name here
# Function to get the current git commit hash
get_git_hash() {
git rev-parse HEAD 2>/dev/null
}
# Function to start the application
start_app() {
git pull origin $BRANCH 2>/dev/null || true
source bin/activate
pip install -r requirements.txt
python init.py &
APP_PID=$!
echo "Application started with PID: $APP_PID"
}
# Function to stop the application
stop_app() {
if [ -n "$APP_PID" ] && kill -0 "$APP_PID" 2>/dev/null; then
echo "Stopping application (PID: $APP_PID)..."
kill "$APP_PID" 2>/dev/null
wait "$APP_PID" 2>/dev/null
fi
}
# Cleanup on script exit
cleanup() {
echo "Shutting down..."
stop_app
exit 0
}
trap cleanup SIGINT SIGTERM
# Initial start
CURRENT_HASH=$(get_git_hash)
start_app
# Watch for changes
CHECK_INTERVAL=10 # seconds between checks
while true; do
sleep $CHECK_INTERVAL
# Fetch updates from remote
git fetch origin 2>/dev/null
# Get the remote hash
REMOTE_HASH=$(git rev-parse origin/$BRANCH 2>/dev/null)
if [ "$CURRENT_HASH" != "$REMOTE_HASH" ]; then
echo "Git changes detected! Restarting..."
stop_app
CURRENT_HASH=$REMOTE_HASH
start_app
fi
done