#!/usr/bin/env bash ############################################################################### # StepProbe — Background Runner # # Launches the full pipeline in the background with: # - nohup for persistence after SSH disconnect # - tmux session for easy re-attach # - Email notification on completion (optional) # # Usage: # bash scripts/run_background.sh # default full run # bash scripts/run_background.sh --quick # quick test # bash scripts/run_background.sh --tmux # use tmux instead of nohup # bash scripts/run_background.sh --notify email # send email on completion ############################################################################### set -euo pipefail PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)" LOGS_DIR="${PROJECT_DIR}/logs" mkdir -p "$LOGS_DIR" TIMESTAMP=$(date +%Y%m%d_%H%M%S) LOG_FILE="${LOGS_DIR}/background_${TIMESTAMP}.log" PID_FILE="${LOGS_DIR}/run.pid" MODE="nohup" EXTRA_ARGS="" NOTIFY_EMAIL="" while [[ $# -gt 0 ]]; do case $1 in --tmux) MODE="tmux"; shift ;; --notify) NOTIFY_EMAIL=$2; shift 2 ;; *) EXTRA_ARGS="$EXTRA_ARGS $1"; shift ;; esac done # Build the command CMD="cd ${PROJECT_DIR} && bash run_all.sh ${EXTRA_ARGS}" # Optional notification on completion if [[ -n "$NOTIFY_EMAIL" ]]; then CMD="${CMD} ; echo 'StepProbe experiment completed at \$(date)' | mail -s 'StepProbe Done' ${NOTIFY_EMAIL}" fi if [[ "$MODE" == "tmux" ]]; then # Check tmux if ! command -v tmux &>/dev/null; then echo "tmux not found. Install with: sudo apt install tmux" echo "Falling back to nohup." MODE="nohup" fi fi case $MODE in tmux) SESSION="stepprobe" # Kill existing session if any tmux kill-session -t "$SESSION" 2>/dev/null || true # Create new session tmux new-session -d -s "$SESSION" "$CMD 2>&1 | tee $LOG_FILE" echo "==================================================================" echo " StepProbe running in tmux session: $SESSION" echo "" echo " Attach: tmux attach -t $SESSION" echo " Detach: Ctrl+B then D" echo " Kill: tmux kill-session -t $SESSION" echo " Monitor: bash scripts/monitor.sh --watch" echo " Log: tail -f $LOG_FILE" echo "==================================================================" ;; nohup) # Launch in background nohup bash -c "$CMD" > "$LOG_FILE" 2>&1 & local_pid=$! echo "$local_pid" > "$PID_FILE" echo "==================================================================" echo " StepProbe running in background" echo "" echo " PID: $local_pid" echo " Log: tail -f $LOG_FILE" echo " Monitor: bash scripts/monitor.sh --watch" echo " Status: ps aux | grep $local_pid" echo " Stop: kill $local_pid" echo "==================================================================" ;; esac