Files
raspi-iot-ryhmatyo/watch.sh
Aaro Varis fe43e4d6ec add watcher
2026-02-05 08:50:54 +02:00

59 lines
1.2 KiB
Bash

#!/bin/bash
# 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
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/$(git rev-parse --abbrev-ref HEAD) 2>/dev/null)
if [ "$CURRENT_HASH" != "$REMOTE_HASH" ]; then
echo "Git changes detected! Restarting..."
stop_app
CURRENT_HASH=$REMOTE_HASH
start_app
fi
done