#!/bin/bash # Watchdog: called by crontab every minute to ensure the monitor script is alive MONITOR_SCRIPT="/home/jenkins/.npu-scripts/npu_auto_occupy.sh" PID_FILE="/home/jenkins/.npu-scripts/npu_auto_occupy.pid" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m' is_running() { if [ -f "$PID_FILE" ]; then local pid=$(cat "$PID_FILE") if kill -0 "$pid" 2>/dev/null && grep -q "npu_auto_occupy.sh" /proc/"$pid"/cmdline 2>/dev/null; then return 0 else rm -f "$PID_FILE" fi fi pgrep -f "npu_auto_occupy.sh" > /dev/null 2>&1 } if ! is_running; then # Start monitor script, redirect output to log echo -e "${YELLOW}[$(date)] Watchdog: Monitor script not running, starting it...${NC}" >> /home/jenkins/.npu-scripts/npu_auto_occupy.log nohup "$MONITOR_SCRIPT" >> /home/jenkins/.npu-scripts/npu_auto_occupy.log 2>&1 & # Wait for PID file to be generated (max 5 seconds) for i in {1..5}; do if [ -f "$PID_FILE" ]; then echo -e "${GREEN}[$(date)] Watchdog: Monitor script started, PID file created${NC}" >> /home/jenkins/.npu-scripts/npu_auto_occupy.log break fi sleep 1 done fi